It is a mechanism, where one class inherits all properties & methods (essential features) of another class. A class from which inheritance, tack place is known as, Base class or super-class or parent class. A class that inherits the all the properties & methods of another class is known as a Derived class or subclass or child class. inherits by keyword “extends”.
Basically, there is three type of inheritance.
i) Single Inheritance ii) Multilevel Inheritance iii) Multiple Inheritance
Example of Single Inheritance:
class A { public int a=10; void show() { System.out.println(a); } }
class B extends A { void display() { System.out.println(a); } }
class Test { public static void main(String arg[]) { B obj=new B(); obj.show(); obj.display(); } }
Example of Multilevel Inheritance :
class A { int a=10; void show() { System.out.println(a); } }
Class B extends A { int b=20; void display1() { System.out.printf(“MultiLavel”); } }
class C extends B { void display() { System.out.println(b+a); } }
class Test { public static void main(String arg[]) { C obj=new C(); obj.show(); obj.display(); obj.display1(); } }
Question and answer is powered by AnsPress.io