Understanding Inheritance

In Java, inheritance allows a class to inherit attributes and methods from another class. Through the 'extends' keyword, subclasses inherit from superclasses, promoting code reuse and hierarchy. Subclasses can override methods, customize behaviors, and access superclass members based on their visibility. Inheritance supports single inheritance and enables the creation of complex class relationships, enhancing code organization and system extensibility within object-oriented programming paradigms.


  • Basic Sysntax of Constructor in Inheritance:
  • The basic syntax of inheritance in Java involves using the extends keyword to establish a subclass and superclass relationship. Here's a concise outline:

    // Superclass
    class Superclass {
     &nbs;// Fields
      // Methods
    }
    // Subclass extending Superclass
    class Subclass extends Superclass {
      // Fields specific to Subclass
      // Methods overriding or additional to Superclass
    }
  • Real life example of Inheritance:
  • Question-1:Write a class with following specifications:
    Class name: employee Data members: String name, float basic;
    Member functions: void getdata(): to accept the name and basic pay Sub Class salary inherit Class employee.
    Data members : float da, hra, gross, net;
    Member Functions: void calculate(): to find the followings:da=50% of basic , hra= 10% of basic , gross= basic + da + hra, pf=8.33% of (basic+da)net = gross – pf.
    void display(): to display the playslip.

    import java.util.*; class Employee{ String name; double basic; void getdata() { Scanner sc=new Scanner(System.in); System.out.println("Enter The name:"); name=sc.next(); System.out.println("Enter the basic:"); basic=sc.nextDouble(); } } class salary extends Employee{ double da,hra,gross,net; void calculate() { da=(basic*50)/100; hra=(basic*10)/100; gross=basic+hra; double pf=((basic+da)*8)/100; net=gross-pf; } void display() { System.out.println("Name="+name); System.out.println("Basic="+basic); System.out.println("Da="+da); System.out.println("hra="+hra); System.out.println("net="+net); } } class Main{ public static void main(String args[]){ salary ob=new salary(); ob.getdata(); ob.calculate(); ob.display(); } }

    OUTPUT:
    Enter The name:
    Namita
    Enter the basic:
    50000
    Name=Namita
    Basic=50000.0
    Da=25000.0
    hra=5000.0
    net=49000.0

    Question-2:. A super class Perimeter has been defined to calculate the perimeter of a parallelogram. Define a subclass Area to compute the area of the parallelogram by using the required data members of the super class. The details are given below:
    Class name: Perimeter. Data members/instance variables: a-to store the length in decimal, b- to store the breadth in decimal. Member functions: Perimeter( … ) : parameterized constructor to assign values to data members double Calculate( ):calculate and return the perimeter of a parallelogram as 2 * (length + breadth). void show(): to display the data members along with the perimeter of the parallelogram.
    Class name: Area. Data members/instance variables: h -to store the height in decimal, area-to store the area of the parallelogram. Member functions: Area( … ) : parameterized constructor to assign values to data members of both the classes. void doarea( ):compute the area as (breadth * height) void show(): display the data members of both classes along with the area and perimeter of the parallelogram.

    Solution:

    import java.util.*; class Perimeter{ double a,b; Perimeter(double x,double y){ a=x; b=y; } double Calculate(){ return (2*(a+b)); } void show(){ System.out.println("Length="+a); System.out.println("Breadth="+b); System.out.println("Perimeter is:"+Calculate()); } } class Area extends Perimeter{ double h,area; Area(double x,double y,double z){ super(x,y); h=z; } void doArea(){ area=(super.b*h); System.out.println("height is:"+h); System.out.println("Area is"+area); } void show(){ super.show(); doArea(); } } class Main4{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println("Enter the height:"); double hi=sc.nextDouble(); System.out.println("Enter the breadth:"); double bre=sc.nextDouble(); System.out.println("Enter the length:"); double len=sc.nextDouble(); Area ob=new Area(len,bre,hi); ob.show(); } }

    OUTPUT:
    Enter the height:
    5
    Enter the breadth:
    6
    Enter the length:
    4
    Length=4.0
    Breadth=6.0
    Perimeter is:20.0
    height is:5.0
    Area is:30.0

  • Conclusion:
  • In Java, inheritance facilitates code reuse and hierarchy creation by allowing subclasses to inherit attributes and methods from superclasses using the 'extends' keyword. Subclasses can override superclass methods and access superclass members. Single inheritance is supported, respecting access modifiers. Constructors are not inherited, but the subclass constructor implicitly calls the superclass constructor. Ultimately, inheritance promotes modularity and maintainability in object-oriented programming.