Answer:
Inheritance provides an object with the ability to acquire the fields and methods of another class, called base class. Inheritance provides reusability of code and can be used to add additional features to an existing class, without modifying it.
Sample class Mammal is shown below which has a constructor. Mammal Class
12345678 | public class Mammal{Â Â public Mammal() Â { Â Â Â System.out.println( "Mammal created" ); } Â } |
Man class extends Mammal which has a default constructor. The sample code is shown below.Man class
1234567 | public class Man extends Mammal{ Â public Man() Â { System.out.println( "Man is created" );Â }Â } |
Inheritance is tested by creating an instance of Man using default constructor. The sample code is shown to demonstrate the inheritance.TestInheritance Class
1234567 | public class Test Inheritance{ public static void main(String args[]) { Man man = new Man();Â }Â } |