Related post

Inheritance in Java programming with example | Zhullyblog

 

Inheritance in Java


INHERITANCE

INHERITANCE as said earlier in the Introduction to Java programming is a process in which a class acquires or inherits the properties and functionalities of another class. 



SYNTAX




class Super {

   //Properties

}

class Sub extends Super {

}

 



From the syntax, there is a line where we have : sub extends super. This means that the sub class inherits the super class.


Inheritance happens in our real world and there is no difference with programming. 

Human being inherit some of parent properties/qualities so,  it works  like that in Java Inheritance.

 The class that inherits the properties and function is called a child class or sub class.

And the class whose properties are inherited are parent class or super class. The inheritance mechanism allows the child class to reuse the properties of the parent class. So the question is,how are the properties of a class Inherited by another?


And that question bring us to ...... extend keyword.




EXTEND KEYWORD

The extend is the keyword used to inherit the properties of a class. Take for instance,  we have two classes ;A and B. 

All we want is to make B acquire the properties of A. To achieve this,we create the class A then create the second class B and use extend keyword to specify the class A we want to inherit . 


Take a good look at this syntax 




class Super {

   //Properties

}

class Sub extends Super {

}

 



Now let take an example 




class Parent {

   String behaviour = "Kind";

   String reaction = "Smile";

   void does(){

System.out.println("Teaching");

   }

}


public class Children extends Parent{

   String parenting = "Parents";

   public static void main(String args[]){

Children obj = new Children();

System.out.println(obj.behaviour);

System.out.println(obj.reaction);

System.out.println(obj.parenting);

obj.does();

   }

}


 


Snippet

Inheritance in Java example


Types of inheritance

There are 4 types of inheritance we have :


SINGLE INHERITANCE

Single Inheritance in java


This Is a direct type of Inheritance where a child /sub class inherits the properties of a parent /super class.


MULTILEVEL INHERITANCE

Multilevel Inheritance in Java


I will simply call this generational Inheritance. This occurs when a sub class inherits from the parents class and also another sub class inherits from the initial subclass.


HIERARCHICAL INHERITANCE

Hierarchical Inheritance in Java


This occurs when more than one class inherits directly from the super class


MULTIPLE INHERITANCE

Multiple Inheritance in Java


: As the name implies,a child class inherits from multiple parent class. Such example is a child inheriting from both mother and father.But java doesn't support multiple Inheritance.





Comments