Special welcome gift. Get 50% off your first courses with code “AS50”. Find out more!

HomeInterviewWhat is Inheritance?

What is Inheritance?

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

12345678public 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

1234567public 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

1234567public class Test Inheritance{public static void main(String args[]){Man man = new Man(); }  }

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

Categories

ads sidebar 1

You May Also Like

Oracle has several modes for shutting down the database: In normal mode, the database is shut down by default. It...
Materialized views are items that contain condensed sets of data from base tables that have been summarized, clustered, or aggregated. They...
Every database in Oracle has a tablespace called SYSTEM, which is generated automatically when the database is created. It also...