Java Inheritance
 
> It is a procedure of Inheriting (reusing) the course of report members (Variables too Methods) from 1 course of report to simply about other course of report is called Inheritance.
> Non static (Object Level) course of report members alone tin hold out inherited.
> The course of report where the course of report members are getting inherited is called equally Super course of report / bring upwards course of report / Base class.
> The course of report to which the course of report members are getting inherited is called Sub course of report / Child course of report / Derived class.
> The inheritance betwixt Super course of report too Sub course of report is achieved using "extends" keyword.
-----------------------------------------------
How to practise static course of report members?
Using "static" not access modifier nosotros tin declare static variables too practise static methods.
How to access Static course of report members?
Using Class get upwards nosotros tin access static course of report members.
How to access Non static course of report members?
Using Object/Instance nosotros tin access Non static course of report members.
----------------------------------
Creating object inwards a class
Syntax:
ClassName InstanceVariable = novel ClassName();
-----------------------------------
Example for accessing static too not static course of report members:
static int a = 10, b = 20; // Static variables
int c = 30, d = 40; // Non static variables
    
world static void add() { //static Method
System.out.println("Addition of a, b is: " + (a+b));
}
    
world void add2() { //Non static method
System.out.println("Addition of c, d is: " + (c+d));
}
        
world static void brain (String [] args) {
// Access Static course of report members using course of report name
System.out.println(FileHandling.a);//10
FileHandling.add();//30
    
//Access Non static course of report members using Object/Instance
FileHandling obj = novel FileHandling();
System.out.println(obj.c);//30
obj.add2();//70
}
--------------------------------------------------
Types of Inheritance
i) Single Inheritance
Ex:
class B extends course of report Influenza A virus subtype H5N1 {
--------------------------------
ii) Multi degree Inheritance
Example:
Class B extends course of report Influenza A virus subtype H5N1 {
Class C extends course of report B {
iii) Multiple Inheritance (* Java doesn't support)
Ex:
Class B extends Class Influenza A virus subtype H5N1 {
Class B extends Class D {
----------------------------
Inheritance Example:
 
Class 1:
public course of report Influenza A virus subtype H5N1 {
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 brain (String [] args){
Influenza A virus subtype H5N1 myObject = novel A();
System.out.println(myObject.a);//10
myObject.addition();//30
}
}
-------------------------
Class 2:
public course of report B extends Influenza A virus subtype H5N1 {
int a = 100;
int b = 200;
world void addition() {
System.out.println("Addition of a, b is: " + (a+b));
}
public static void brain (String [] args){
Influenza A virus subtype H5N1 obj1 = novel Influenza A virus subtype H5N1 ();
obj1.addition(); //30
obj1.subtraction();//-10
    
B obj2 = novel B();
obj2.addition();//300
}
}
-------------------------------------
Class 3:
public course of report C extends B {
int a = 1;
int b = 2;
world void addition() {
System.out.println("Addition of a, b is: " + (a+b));
}
public static void brain (String [] args){
//Accessing course of report members from Home class
C obj1 = novel C();
System.out.println(obj1.a); //1
obj1.addition();//3
    
//Accessing course of report members from bring upwards class
B obj2 = novel B();
obj2.addition();//300
    
//Accessing course of report members from Grandparent class
Influenza A virus subtype H5N1 obj3 = novel A();
obj3.addition(); // 30
}
}
----------------------------------------------------
  Sumber http://www.gcreddy.com/
> It is a procedure of Inheriting (reusing) the course of report members (Variables too Methods) from 1 course of report to simply about other course of report is called Inheritance.
> Non static (Object Level) course of report members alone tin hold out inherited.
> The course of report where the course of report members are getting inherited is called equally Super course of report / bring upwards course of report / Base class.
> The course of report to which the course of report members are getting inherited is called Sub course of report / Child course of report / Derived class.
> The inheritance betwixt Super course of report too Sub course of report is achieved using "extends" keyword.
-----------------------------------------------
How to practise static course of report members?
Using "static" not access modifier nosotros tin declare static variables too practise static methods.
How to access Static course of report members?
Using Class get upwards nosotros tin access static course of report members.
How to access Non static course of report members?
Using Object/Instance nosotros tin access Non static course of report members.
----------------------------------
Creating object inwards a class
Syntax:
ClassName InstanceVariable = novel ClassName();
-----------------------------------
Example for accessing static too not static course of report members:
static int a = 10, b = 20; // Static variables
int c = 30, d = 40; // Non static variables
world static void add() { //static Method
System.out.println("Addition of a, b is: " + (a+b));
}
world void add2() { //Non static method
System.out.println("Addition of c, d is: " + (c+d));
}
world static void brain (String [] args) {
// Access Static course of report members using course of report name
System.out.println(FileHandling.a);//10
FileHandling.add();//30
//Access Non static course of report members using Object/Instance
FileHandling obj = novel FileHandling();
System.out.println(obj.c);//30
obj.add2();//70
}
--------------------------------------------------
Types of Inheritance
i) Single Inheritance
Ex:
class B extends course of report Influenza A virus subtype H5N1 {
--------------------------------
ii) Multi degree Inheritance
Example:
Class B extends course of report Influenza A virus subtype H5N1 {
Class C extends course of report B {
iii) Multiple Inheritance (* Java doesn't support)
Ex:
Class B extends Class Influenza A virus subtype H5N1 {
Class B extends Class D {
----------------------------
Inheritance Example:
Class 1:
public course of report Influenza A virus subtype H5N1 {
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 brain (String [] args){
Influenza A virus subtype H5N1 myObject = novel A();
System.out.println(myObject.a);//10
myObject.addition();//30
}
}
-------------------------
Class 2:
public course of report B extends Influenza A virus subtype H5N1 {
int a = 100;
int b = 200;
world void addition() {
System.out.println("Addition of a, b is: " + (a+b));
}
public static void brain (String [] args){
Influenza A virus subtype H5N1 obj1 = novel Influenza A virus subtype H5N1 ();
obj1.addition(); //30
obj1.subtraction();//-10
B obj2 = novel B();
obj2.addition();//300
}
}
-------------------------------------
Class 3:
public course of report C extends B {
int a = 1;
int b = 2;
world void addition() {
System.out.println("Addition of a, b is: " + (a+b));
}
public static void brain (String [] args){
//Accessing course of report members from Home class
C obj1 = novel C();
System.out.println(obj1.a); //1
obj1.addition();//3
//Accessing course of report members from bring upwards class
B obj2 = novel B();
obj2.addition();//300
//Accessing course of report members from Grandparent class
Influenza A virus subtype H5N1 obj3 = novel A();
obj3.addition(); // 30
}
}
----------------------------------------------------