Skip to main content

OOPS Concept in Java : INHERITANCE

 JAVA OOPS CONCEPT INHERITANCE ( SESSION 2 ) :

From the word INHERITANCE in java only it's clear that this is  related to inheritance of properties and behaviours of a class. So class which is trying to inherit properties and behaviours of other class is going to called as Child class and the class from where its going to inherit will be the Parent class.

Let's consider an example as below.

public class PARENT {

int a=10;

int b=20;

public int sum(int fNumber, int sNumber){

int result=fNumber + sNumber;

return result;

    }
}


class CHILD extends PARENT{

int a=60;

int b=50;

public int subtract(int fNumber, int sNumber){

int result=fNumber - sNumber;

return result;

           }

public static void main(String [] args){

    PARENT parent=new PARENT();

    CHILD child=new CHILD();

    System.out.println(parent.sum(parent.a,parent.b));

    System.out.println(child.sum(child.a,child.b));

    System.out.println(child.subtract(child.a,child.b));


        }

}

Output of this program will be : 30, 110 , 10

Last 10 is from CHILD class method i.e subtract(60-50) and first 30 is from PARENT class sum(10,20) method, but how come we got sum of 60 and 50 as 110 from child class call of method sum(60,50), its because of keyword extends mentioned while creating CHILD class. We told CHILD class to inherit properties and Behaviour of Parent that's the reason without having sum method still CHILD is able to use sum() method from PARENT class along with its own subtract method().

I already explained Properties and Behaviour of a class, if you have doubt please go through our previous post JAVA BASIC OVERVIEW 

As we all know Parent knows how to handle child because he is the father of son and son is new to this world and less experience than father . Same way below 2 line can be understood that PARENT reference can hold the CHILD Object while CHILD reference can't hold the PARENT Object .

CHILD parent1=new PARENT(); //This is compile time error

 PARENT child1=new CHILD(); //This is possible 

In Red line we have tried to create Object of PARENT class and try to keep in reference of CHILD class type which is not possible while in Green line we have created Object of CHILD class and kept the reference in PARENT class type.

Let's create the same sum() method as PARENT class in CHILD class with addition of extra 1, now both PARENT and CHILD has same sum() method only difference inside block with extra 1. 

public int sum(int fNumber, int sNumber){
    int result=fNumber + sNumber
+ 1;
    return result;
}

Now let's consider correct green line object creation of CHILD object and keeping the reference in PARENT class.

So what will be output of below lines consider PARENT and CHILD class as above : 

PARENT child=new CHILD();

System.out.println(child.a); // This will print 10 (Properties of Parent class)

System.out.println(child.b); //This will print 20 (Properties of Parent class)

System.out.println(child.sum(child.a,child.b)); // This will print 31 not 30,  this proved that CHILD class method has been called not PARENT class.

 Because Behaviour can be overridden like if Father knows swimming then son can try to be expert in by improvement because its behaviour but the properties are constant in person, if father has no hair it means he is bald and its his father properties  and i don't think any son wants to inherit this properties. Only Behaviour should be improved by son while both will have their own properties like father is Tall while son is not etc.

  That's the reason after creating object of CHILD class also while calling a and b its printing 10 and 20 of PARENT class because reference is of PARENT class but in case of method its child method because its a behaviour which son has tried to improved it.

Based on the above explanation , we can't have multiple Parents , we can have Father , GrandFather but not multiple father that's the reason JAVA doesn't support multiple Inheritance while Multi-Level Inheritance is possible. Because JAVA will get confused if same sum() method is in 2 Parent class then which one should be executed.

 Every Father is the super-hero for his son, so to use properties and behaviour of father by son, we will have to use a keyword called super() and this should be called  as first line child class constructor as below.

class CHILD extends PARENT{

int a=60;
int b=50;

    CHILD(){
        System.out.println(super.sum(super.a,super.b));
}

while creating Object of Child class automatically parent class sum() method will be executed and it will print 30.

  Please let me know in comment section if you have any doubt. I also request you to go through our previous post on OOPS concept from below link.


Comments

Popular posts from this blog

Bubble sort Implementation

Bubble sort  : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). // Bubble Sort class BubbleSort { public static void sort ( int [] array) { int n = array. length ; while ( true ) { boolean swapped = false; for ( int i = 0 ; i < n - 1 ; i++) { if (array[i + 1 ] < array[i]) { swap (array , i , i + 1 ) ; swapped = true; } } if (!swapped) break; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ; array[i] = array[j] ;

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics     } } In above class , we have define 2 numbers one is holding 10 and another is holdin

Overview of time and space complexity for sorting algorithm

Bubble sort : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). Selection sort : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.      Time Complexity for selection sort also is O(n^2).      space complexity for bubble sort will be O(1). Insertion sort :  In insertion sort, Same process like selection sort , we will divide the array in sorted and unsorted array and we will select element from unsorted array and will insert it into sorted array at its proper position.    Time Complexity for Insertion sort also is O(n^2)