Java Inheritance

Learn from my YouTube Video – Java Inheritance

Also learn how to setup selenium and write first program

  • In simple term inheritance is nothing but parent-child relationship to accessing once class members into another class.

First to understand Static and Non-Static Class Member and How to access Static and Non-Static class members (Variables and Methods)


Example:

public class Class1 {

      //Static member

      static int a=10, b=20;

      //Non-Static member

      int c=30, d=10;

      //Create Static member with retuning value

      public static int sum(int a, int b)      {

         int result = a+b;

         return result;

      }

      //Create Static member without returning value

      public static void mul (int a, int b){

            System.out.println(a*b);

      }

      //Create Non-Static member with retuning value

      public int add2(int c, int d){

            int result = c + d;

            return result;

      }

      public static void main(String [] args)  {

       //Access Static Class variable and methods

     int x = Class1.sum(a, b);

     System.out.println("Result : " +x); //Result : 30

       Class1.mul(a, b); //Result : 200 OR

       mul(a,b);

       // Access Non Static Class variable and methods

        // add2(c,d); // Can't allowed to access Non Static Class member

       Class1 obj = new Class1();

       System.out.println(obj.add2(obj.c, obj.d));

      }

 }

Result : 30
200
200
40

Three leval of Inheritance in Java

  1. Single inheritance

Ex. public class A extends B (Parent-Child concept)

single_inheri

2. Multi-Level Inheritance

Ex. public class B extends A

public class C extends B

multi-level

Q – If class A and B having same class members the how class C consider?

A – First priority goes to same class than immediate parent and last on Grand Parent

3. Multiple Inheritance

Note – Java does support multiple inheritance due to diamond problem

Ex. public class C extends A, B

multi-level_001

Inheritance:

  • Inheriting the class members (Variables and Methods) from one class to another.
  • Important term
    • Super Class/Parent Class/Base Class – The class which is inheriting into other class means reusing variables and method of this class
    • Sub Class/Child Class/Derived Class – The class which is inherited Parent class in order reuse the variable and method of Parent class
  • Using ‘extends’ keyword inheritance possible between Super and Sub class
  • A subclass inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements.

class subclass-name extends superclass-name
{
// Body of the class
}

Example :

public class A { 
 // Here A is Parent/Super/Base Class
 
 int a = 10;
 int b = 20;

public void display(){ 
 System.out.println("This is the display method of Class A");
 } 
 public static void main(String[] args) {
 
 A obj = new A();
 System.out.println("Print a :"+obj.a); //Accessing non-static variable of same class
 System.out.println("Print b :"+obj.b); 
 obj.display(); //Accessing non-static method of same class
 }
}
public class B extends A{
 // Here B is Child Class of Parent A
// int a = 30;
 int b = 40;
 
 public void display(){ 
 System.out.println("This is the display method of Class B");
 super.display(); // Accessing non-static method of Parent Class A
 }
 public static void main(String[] args) {
 
 B objB = new B();
 System.out.println("Print Class B variable a :"+objB.a); // Accessing non-static variable of same Class A
 System.out.println("Print Class B variable b :"+objB.b); // Accessing non-static variable of same Class B
 objB.display(); // Accessing non-static method of same class B
 
 }
}
public class C extends B{ //Multi-level inheritance

// Here C is Sub-sub class of Grand Parent A
 // B is Parent Class of Class C
 // B is Sub Child Class of Class A
 public static void main(String[] args) {
 C objC = new C();
 System.out.println("Print Class A variable a :"+objC.a); //Accessing non-static variable of Grand Parent A
 objC.display(); //Accessing non-static method of Parent B
 }

}

Output of Class C Result :

Print Class B variable a :10
This is the display method of Class B
This is the display method of Class A

 

Advertisements

About samp79

Professional Automation Tester
This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s