Inheritance in Java programming with example | Zhullyblog

4 years ago 16

  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 Types of inheritance There are 4 types of inheritance we have : SINGLE INHERITANCE:  This Is a direct type of Inheritance where a child /sub class inherits the properties of a parent /super class. MULTILEVEL INHERITANCE 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 This occurs when more than one class inherits directly from the super class MULTIPLE INHERITANCE : 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. Java Basics Introduction to Java programming Java Syntax How to declare a variable in Java Java data types Java Operators If, if..else statement in Java Java For loop Break statement in Java Continue statement in Java Java Object Oriented Programming Please share  Follow us on Twitter , Facebook page , Facebook group Other Tutorials HTML JavaScript PHP JAVA


View Entire Post

Read Entire Article