X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Inheritance :

Inheritance is the technique which allows us to inherit the data members and methods from base class to derived class.

Base class is one which always gives its features to derived classes.

Derived class is one which always takes features from base class. A Derived class is one which contains some of features of its own and some of the data members from base class.

The concept of Inheritance is also known as sub-classing or derivation or extendible classes or reusability.

Syntax for inheriting the features from base class to derived class : class <derived_class_name> extends <base_class_name> { variable declaration; method definition; } // extends is a keyword which is used for inheriting the data members and methods from base class to the derived class and it also improves functionality of derived class.

Example :

class Base; { int a; void m1() { } } class Derived extends Base { int b; void m2() { } }

Note :

  • Whatever the data members are coming from base class to the derived class, the base class methods are logically defined in derived class.

  • private data members and private methods of the base class will not be inherited at all.

Advantages of Inheritance :


  • Application development time is very less.

  • Redundancy (repetition) of the code is reducing. Hence we can get less memory cost and consistent results.

  • Instrument cost towards the project is reduced.

  • We can achieve the slogan Write One's Reuse/Run Anywhere (WORA) of Java.

Types of Inheritance (Reusable techniques):

Based on taking the features from base class to the derived class, in Java we have five types of inheritances. They are as follows:

  • Single Inheritance




  • Multi Level Inheritance




  • Hierarchical Inheritance




  • Multiple Inheritance




  • Hybrid Inheritance





Single Inheritance :

Single inheritance is one in which there exists single base class and single derived class.

class Base
{

}
class Derive extends Base
{

}

Multi Level Inheritance :

Multi level inheritance is one in which there exist single base class, single derived class and many intermediate base classes.

An intermediate base class is one which in one context acts as base class and in another context acts as derived class.

class Base
{

}
class Intermediate1 extends Base
{

}
class Intermediate2 extends Intermediate1
{

}
class Derive extends Intermediate2
{

}

Hierarchical Inheritance :

Hierarchical inheritance is one in which there exists single base class and multiple derived classes.

class Base
{

}
class Derive1 extends Base
{

}
class Derive2 extends Base
{

}
class Derive3 extends Base
{

}

Multiple Inheritance :

Multiple inheritance is one in which there exists multiple base classes and single derived class.

Multiple inheritances are not supported by Java through classes but it is supported by Java through the concept of Interface.

class Base1
{

}
class Base2
{

}
class Base3
{

}
class Derive extends Base1 , Base2 , Base3
{

} // Invalid in Java

Hybrid Inheritance :

Hybrid inheritance is the combination of any available inheritance types.

In the combination, if one of the combination is multiple inheritance then the entire combination will be not supported by classes in Java but it is supported by the concept of Interface.

class Base
{

}
class Derive1 extends Base
{

}
class Derive2 extends Base
{

}
class Derive extends Derive1 , Derive2
{

} // Invalid in Java
class Base1
{

}
class Base2 extends Base1
{

}
class Derive1 extends Base2
{

}
class Derive2 extends Base2
{

} // Valid in Java

Important Points :


  • extends is a keyword used for inheriting features of base class into derive class and it improves the functionality of derived class.




  • In Java programming, one derived class can extend only one base class because Java never supports multiple inheritance through the concept of classes but it can be supported through the concept of Interface.




  • Whenever we develop any application it is always recommended to create an object of bottommost derived class because it inherits the features of topmost base class and intermediate base classes.




  • When we create an object of bottommost derived class then firstly we get memory space for data members of topmost base class then we get memory space for data members of intermediate base class and lastly we get memory space for data members of bottommost derived class.




  • If we don't want to give features of base class into derived class then definition of base class must be made as final. Hence, final base classes never participates in inheritance.




  • If we don't want to give some features of the base class into derived class then such features must be made as private. Hence, private features of base class never participate in inheritance or never visible/accessible in the context of derived class.




  • In inheritance, data members and methods of base class can be inherited but constructors of the base class cannot be inherited into the context of the derived class because every constructor is meant for initializing the data members of its own class.




  • By using an object of base class we can access all the features which are present in the same base class but an object of base class cannot access those features which are specially defined in its subclass (this concept is known as scope of base class object).




  • For each and every class in Java there exists an implicit predefined super class called java.lang.Object because it provides garbage collection facility for collecting unused memory space for improving performance of Java applications.




Example :

class Algo { void m1() { System.out.print("Algo"); } } class Valley extends Algo { void m2() { System.out.print("Valley"); } } class Example { public static void main(String [ ] args) { Valley v = new Valley(); v.m1(); v.m2(); } }

Output :

//save as : Example.java //compile as :javac Example.java //run as : java Example AlgoValley


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.