JAVA Inheritance Programs Examples [Volume 1]

JAVA Inheritance Programs Examples [Version 1]

1. Create a class named 'Member' having the following members:
Data members:
a) Name
b) Age
c) Phone number
d) Address
e) Salary
Member Methods:
It has a method named 'printDetails()' which prints the details of the members.
Two classes 'Employee' and 'Manager' inherits the 'Member' class. The 'Employee' and 'Manager' classes have data members 'specialization' and 'department' respectively. And two classes have a method named ‘getData()’ which collects values for the data members. Now, assign the name, age, phone number, address, and salary to an employee and a manager by making an object of both of these classes and printing the same.

Ans:

import java.util.Scanner; //Importing Scanner class for taking inputs from the user
class Member
{
    String Name; //data members
    int Age;
    int Phone;
    String Address;
    int Salary;
}
class Employee extends Member //Child Employee of Parent Member
{
    String Specialization; //its own data member
    public void getData() throws Exception //its own method, it throws if an IO exception occurs
    {
        Scanner sc=new Scanner(System.in); //Scanner's object is sc
        System.out.print("Enter Name :");
        Name=sc.nextLine();
        System.out.print("Enter Address :");
        Address=sc.nextLine(); //nextLine() method to receive String values
        System.out.print("Enter Specialization :");
        Specialization=sc.nextLine();
        System.out.print("Enter Age :");
        Age=sc.nextInt();
        System.out.print("Enter Phone No :");
        Phone=sc.nextInt(); //nextInt() method to receive Integer values
        System.out.print("Enter Salary :");
        Salary=sc.nextInt();
    }
    public void printDetails() //member method for display
    {
        System.out.println("Employee Records");
        System.out.println("Name =" + Name);
        System.out.println("Age =" + Age);
        System.out.println("Phone =" + Phone);
        System.out.println("Address =" + Address);
        System.out.println("Salary =" + Salary);
        System.out.println("Specialization =" + Specialization);
    }
}
class Manager extends Member //Child Manager of Parent Member
{
    String Department;
    public void getData() throws Exception
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter Name :");
        Name=sc.nextLine();
        System.out.print("Enter Address :");
        Address=sc.nextLine();
        System.out.print("Enter Department :");
        Department=sc.nextLine();
        System.out.print("Enter Age :");
        Age=sc.nextInt();
        System.out.print("Enter Phone No :");
        Phone=sc.nextInt();
        System.out.print("Enter Salary :");
        Salary=sc.nextInt();
    }
    public void printDetails()
    {
        System.out.println("Manager Records");
        System.out.println("Name =" + Name);
        System.out.println("Age =" + Age);
        System.out.println("Phone =" + Phone);
        System.out.println("Address =" + Address);
        System.out.println("Salary =" + Salary);
        System.out.println("Department =" + Department);
    }
}
class MainMember{ //Main class
    public static void main(String[] args) {
        try //handling if any exception occurs
        {
            Employee e=new Employee(); //creating object of Employee
            e.getData();
            e.printDetails();
            Manager m=new Manager(); //creating object of Manager
            m.getData();
            m.printDetails();
        }
        catch(Exception x)
        {
            System.out.println(x);
        }
    }
}

Output:-

output of java inheritance program 1

In the above program, class Member has two child classes - Employee and Manager. So, these two classes inherit the member variables of the parent class, taking data from the user and printing those values accordingly.

2. Create a class named 'Rectangle' with two data members 'length' and 'breadth' and two methods to print the area and perimeter of the rectangle respectively. Its constructor having parameters for length and breadth is used to initialize the length and breadth of the rectangle. 
Let class 'Square' inherit the 'Rectangle' class with its constructor having a parameter for its side (suppose s) calling the constructor of its parent class using super. Print the area and perimeter of a rectangle and a square. Then also print the area of 10 squares using an array of objects.

Ans:

class Rectangle
{
    int length, breadth; //member variables
    Rectangle(int x, int y) //parameterized constructor
    {
        length=x; breadth=y;
    }
    void showArea() //showing area through method
    {
        System.out.println("Area ="+ (length*breadth));
    }
    void showPerimeter() //showing perimeter through method
    {
        System.out.println("Perimeter ="+ (2*(length+breadth)));
    }
}
class Square extends Rectangle //Square inheriting the Rectangle
{
    Square(int s) //its own parameterized constructor
    {
        super(s,s); //calling the parent class's constructor with the parameter s by using the super concept
    }
}
class MainShape
{
    public static void main(String args[])
    {
        Rectangle r=new Rectangle(5,7); //creating rectangle object r
        r.showArea(); // area= 5x7
        r.showPerimeter(); //perimeter = 2x(5+7)
        Square s=new Square(3); //creating square object s
        s.showArea(); // area= 3x3
        s.showPerimeter(); perimeter = 2x(3+3)
        Square[] a = new Square[10]; //creating array of object, a[10]
        int k = 5; //beginning the parameter value from 5
        for(int i = 0;i<10;i++){
            a[i] = new Square(k); //memory allocation for the objects
            k++;
        }
        for(int i = 0;i<10;i++){
            a[i].showArea(); //calling the method by all the objects
            a[i].showPerimeter();
    }
    }
}

Output:-


output of java inheritance program 2

Here, the Square class is the child class of the Rectangle class. So, class Square is inheriting the two methods showArea and showPerimeter easily. Using the super() with a parameter value of one side(s) the class Square is calling the Rectangle's parameterized constructor to initialize the length and breadth of the square with s, which acts like length = breadth = s.

So, when we are calling the methods from the main class, we are getting the appropriate outputs. Now, we have to make an array of objects. Here, a is the array of Square class of 10 sizes.

Each time, when memory allocation is done for the array of objects, the parameter starts with the side value 5 and then increases by 1. So next objects are getting size value from the parameter k as 5,6,7,8,...

Thus it is giving us the area and parameter value for those sides where a parameter is 5, 6, 7, then 8, etc.,