Search This Blog

Monday 25 July 2022

7. Java Program to implement Single Inheritance with method overriding.

 /* 7. 2019Syllabus Consider class person with fields name, address and date of birth and methods read_data() and show() and another class employee inherited form person class with fields emp_id, date of join and salary and methods read() and show(). Write java program to implement the concept of single inheritance with method overriding concepts for the above classes.*/

import java.io.*;
class person
{
    String name, address, dob;
    public void read_data() throws IOException
    {
        DataInputStream in=new DataInputStream(System.in);
        System.out.println("Enter Name, Address and Date of Birth:");
        name=in.readLine();
        address=in.readLine();
        dob=in.readLine();
        System.out.println("Name"+name);
    }
    public void show()
    {
        System.out.println("\nDetails of employee from person class");
        System.out.println("Name:"+name);
        System.out.println("Address:"+address);
        System.out.println("Date of Birth:"+dob);
    }
    
}
class employee extends person
{
    String emp_id,doj;
    float salary;
    public void read()throws IOException
    {
        DataInputStream in=new DataInputStream(System.in);
        System.out.println("Enter Employee ID, Date of Joining and Salary:");
        emp_id=in.readLine();
        doj=in.readLine();
        salary=Float.parseFloat(in.readLine());
    }
    //Comment this method to check for method overriding
    public void show()
    {
        System.out.println("\nDetails of Employee from employee class");
        System.out.println("Emp_ID:"+emp_id);
        System.out.println("Date of Joining:"+doj);
        System.out.println("Salary:"+salary);
    }
}
class Prog71
{
    public static void main(String args[])throws IOException
    {
        employee eobj=new employee();
        eobj.read_data();
        eobj.read();
        eobj.show(); // show() method is overridden.
    }
}

No comments :

Post a Comment

FREE Hit Counters