Inheritance: ============ When we can use Inheritance? ============================ while creating the class if we want to define data and methods which are from another class without re-creating/re-definition. In this case, we can use "Inheritance". "Creating the class from another class by inherit the properties and methods is called as "Inheritance". payment{ paymentId; paymentDate; Amount; } UPIPayment extends payment{ UPIId; } cardPayment extends payment{ cardtype; cardHolderName; } ==> To implement the inheritance "extends" keyword can be used. ==> the class which is taking the data/methods from another class is called as "child class or Derived class or sub class". ==> from which class the class accepting the data that class is called as "parent class or super class base/master class". Types of Inheritance: ==================== ==> 5-types: 1) Single Inheritance 2) Multi-level Inheritance 3) Multiple Inheritance 4) Hierarchical Inheritance 5) Hybrid Inheritance 1) Single Inheritance ====================== ==> there is one parent class and only one child class. ==> the object of child class can access the data from its own class and also from the parent class. class Camera{ brand; resolution; cost; } class DigitalCamera extends Camera{ memory } package OOP.IO.AIT; //parent/super/base class class Camera{ private String brand; private String resolution; private double cost; // Parameterized Constructor public Camera(String brand, String resolution, double cost) { super(); // super call: to call the parent class constructor this.brand = brand; this.resolution = resolution; this.cost = cost; // super(); } public void display() { System.out.println("The Brand Name = "+brand); System.out.println("The Resoultion = "+resolution); System.out.println("Cost of the Camera = "+cost); } } // child/sub/derived class class DigitalCamera extends Camera{ private String memory; public DigitalCamera(String brand, String resolution, double cost, String memory) { super(brand, resolution, cost); this.memory = memory; } public void printDetails() { display(); System.out.println("Memory = "+memory); } } public class MainClass { public static void main(String[] args) { DigitalCamera dc = new DigitalCamera("Kodak","100 MP",200000.0,"5 GB"); // dc.display(); dc.printDetails(); } } ================================================== super() call: ============= 1) super() call can call the parent class constructor. 2) we can add super() call always as the first statement in the constructor. 3) if we have not defined the super() call, the JVM can automatically write super(). package OOP.IO.AIT; class MyClass{ private int x; private double y; MyClass(int x,double y) { super(); this.x = x; this.y = y; } public void display() { System.out.println("x = "+x); System.out.println("y = "+y); } } public class Main { public static void main(String[] args) { MyClass mc = new MyClass(1122,1020.0); mc.display(); } } ============================================================== Multi-level Inheritance: ======================== class Loan{ } class HomeLoan extends Loan{ } class CarLoan extends HomeLoan{ } package OOP.IO.AIT; class Parent{ private int x; private double y; public Parent(int x, double y) { super(); this.x = x; this.y = y; } public void display1() { System.out.println("x = "+x); System.out.println("y = "+y); } } class Child1 extends Parent{ private float z; public Child1(int x, double y, float z) { super(x, y); this.z = z; } public void display2() { display1(); System.out.println("z = "+z); } } class Child2 extends Child1{ public Child2(int x, double y, float z) { super(x, y, z); } public void display3() { // display1(); display2(); } } public class InheritaanceMainClass { public static void main(String[] args) { Child2 cobj = new Child2(123,1024.0,1.23067f); // cobj.display1(); // cobj.display2(); cobj.display3(); } }