Java Inheritance

Java Inheritance
 

> It is a process of Inheriting (reusing) the class members (Variables and Methods) from one class to another class is called Inheritance.

> Non static (Object Level) class members only can be inherited.

> The class where the class members are getting inherited is called as Super class / parent class / Base class.

> The class to which the class members are getting inherited is called Sub class / Child class / Derived class.

> The inheritance between Super class and Sub class is achieved using "extends" keyword.
-----------------------------------------------
How to create static class members?
Using "static" non access modifier we can declare static variables and create static methods.

How to access Static class members?
Using Class name we can access static class members.

How to access Non static class members?
Using Object/Instance we can access Non static class members.
----------------------------------
Creating object in a class
Syntax:

ClassName InstanceVariable = new ClassName();
-----------------------------------
Example for accessing static and non static class members:
static int a = 10, b = 20; // Static variables
    int c = 30, d = 40; // Non static variables
   
    public static void add() { //static Method
        System.out.println("Addition of a, b is: " + (a+b));
    }
   
    public void add2() { //Non static method
        System.out.println("Addition of c, d is: " + (c+d));
    }
       
        public static void main (String [] args) {
    // Access Static class members using class name
    System.out.println(FileHandling.a);//10
    FileHandling.add();//30
   
    //Access Non static class members using Object/Instance
    FileHandling obj = new FileHandling();
    System.out.println(obj.c);//30
    obj.add2();//70
}
--------------------------------------------------
Types of Inheritance
i) Single Inheritance
Ex:

class B extends class A {
--------------------------------
ii) Multi level Inheritance

Example:

Class B extends class A {

Class C extends class B {

iii) Multiple Inheritance (* Java doesn't support)

Ex:

Class B extends Class A {

Class B extends Class D {
----------------------------

Inheritance Example:

Class 1:
public class A {
int a = 10;
int b = 20;
public void addition() {
    System.out.println("Addition of a, b is: " + (a+b));
}
public void subtraction() {
    System.out.println("Subtraction of a, b is: " + (a-b));
}
public static void main (String [] args){
    A myObject = new A();
    System.out.println(myObject.a);//10
    myObject.addition();//30
}
}
-------------------------
Class 2:
public class B extends A {
    int a = 100;
    int b = 200;
    public void addition() {
        System.out.println("Addition of a, b is: " + (a+b));
    }
public static void main (String [] args){
    A obj1 = new A ();
    obj1.addition(); //30
    obj1.subtraction();//-10
   
    B obj2 = new B();
    obj2.addition();//300
}
}
-------------------------------------
Class 3:
public class C extends B {
    int a = 1;
    int b = 2;
    public void addition() {
        System.out.println("Addition of a, b is: " + (a+b));
    }
public static void main (String [] args){
    //Accessing class members from Home class
    C obj1 = new C();
    System.out.println(obj1.a); //1
    obj1.addition();//3
   
    //Accessing class members from parent class
    B obj2 = new B();
    obj2.addition();//300
   
  //Accessing class members from Grandparent class
    A obj3 = new A();
    obj3.addition(); // 30
}
}
----------------------------------------------------

0 comments:

Post a Comment