4) “A Person has First Name, Last Name, Mobile Number & age. An Employee has Employee ID & Salary. Also, Employee IS-A Person.” Using the concept of inheritance, write a Java Program that displays all the attributes of Employee and Person for the given problem scenario.
package Program4;
class person {
String firstname, lastname;
double mobileno, age;
person(String fname, String lname, double n, double m) {
firstname = fname;
lastname = lname;
mobileno = n;
age = m;
}
void show() {
System.out.println("Firstname: " + firstname);
System.out.println("Lastname: " + lastname);
System.out.println("Mobile: " + mobileno);
System.out.println("Age: " + age);
}
}
class employee extends person {
double empid, Sal;
employee(String fname, String lname, double n, double m, double a, double b) {
super(fname, lname, n, m);
empid = a;
Sal = b;
}
void show() {
System.out.println("Firstname: " + firstname);
System.out.println("Lastname: " + lastname);
System.out.println("Mobile: " + mobileno);
System.out.println("Age: " + age);
System.out.println("empid:" + empid);
System.out.println("Sal:" + Sal);
}
}
public class Test {
public static void main(String args[]) {
person obj = new person("amit", "sharma", 8660, 20);
obj.show();
employee obj1 = new employee("amit", "sharma", 8660, 20, 1, 12000);
obj1.show();
}
}
0 comments:
Post a Comment