Sunday, 25 June 2017

Extending a class in java - Single level Inheritance ~ foundjava

Inheritance concept is encouraged in java as it is an OOP language.
class A
{
    public void printA()
    {
    System.out.println("I am in class A");
    }
}
class B extends A
{
    public void printB()
    {
    System.out.println("I am in class B");
    }
}
class Main
{
    public static void main(String args[])
    {
    A a=new A();
    a.printA();
    // a.printB(); error

    B b=new B();
    b.printA();
    b.printB();   
    }
}
class A : Internally inherits java.lang.Object
class B : Inherits A
class Main : In which main() is written.

No comments:

Post a Comment