Question

Write a Java program which creates a class named 'Employee' having the following

members: Name, Age, Phone number, Address, Salary. It also has a method named 'printSalary( )' which prints the salary of the Employee. 


Two classes 'Officer' and 'Manager' inherits the 'Employee' class.


The 'Officer' and 'Manager' classes have data members 'specialization'

and 'department' respectively.


Now, assign name, age, phone number, address and salary to an officer and a manager by making an object of both of these classes and print the same. (Exercise to understand inheritance).


//Employee.java

public class Employee
{
private String name;
private int age;
private String phoneNo;
private String address;
private int salary;

public Employee(String name, int age, String phoneNo,String address, int salary)
{
this.name = name;
this.age = age;
this.phoneNo = phoneNo;
this.address = address;
this.salary = salary;
}

public void printSalary()
{
System.out.println("Salary of " + name + " is " + salary);
}
public void print()
{
System.out.println("name = " + name);
System.out.println("age = " + age);
System.out.println("phoneNo = " + phoneNo);
System.out.println("address = " + address);
System.out.println("salary = " + salary);
}
}

//InheritenceDemo.java
public class InheritanceDemo
{
public static void main(String[] args)
{
Officer o = new Officer("Ajith",25, "9497111111","Muliyar Kasaragod", 25000,"Computer Science Engineering");
Manager m = new Manager("Rahul",30, "9846222222","Marine Drive Cochin",40000, "HR");

o.print();
System.out.println();
m.print();
}
}


//Manager.java
public class Manager extends Employee
{
private String department;

public Manager(String name, int age, String phoneNo,String address, int salary, String department)
{
super(name, age, phoneNo, address, salary);
this.department = department;
}

public void print()
{
super.print();
System.out.println("department = " +department);
}
}

//Officer.java
public class Officer extends Employee
{
private String specialization;

public Officer(String name, int age, String phoneNo,String address, int salary, String specialization)
{
super(name, age, phoneNo, address, salary);
this.specialization = specialization;
}

public void print()
{
super.print();
System.out.println("specialization = " + specialization);
}
}

Comments

Popular posts from this blog

MatrixMultiplication

FrequencyCountDemo