In: Computer Science
Question1: Define a class Human that contains: - The data fields "first name" and "last name" - A constructor that sets the first and last name to the instance variables. Create also a no-argument constructor - A getName() method which prints the first and last name Define the class Student which inherits Human and contains: - Private data field "mark" of type integer and a constructor which calls the superclass constructor Define the class Worker which inherits Human and contains: - Private date fields "wage" and "hours worked", and a constructor which calls the superclass constructor - A setWageInfo() method which sets the wage information to the instance variables - A calculateWage() method which calculates a worker’s salary based on wage and hours worked Write a program called HumanTest that instantiates two Human objects (one using the noargument constructor and another using the constructor with parameters). Call the method getName(). Create a Student object which calls the getName() method. Create a Worker object which calls the methods getName(), setWageInfo(), and claculateWage().
Default Constructor(No arg. Constructor):
A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class.
Parameterized Constructor(Constructor With Arguments):
A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with your own values, then use a parameterized constructor.
Note:-
To call any constructor or make use of constructor in Derived Class we make use of SUPER() keyword in a program where if there is an parameterized constructor. pass arguments first to the Base Class Constructor as per sequence and then to the same class constructor.
Solution to the above example is shown below.....
class Human//parent class
{
String first_name;
String last_name;
Human()//Default Constructor also called No arg.
Constructor
{
first_name="NULL";
last_name="NuLL";
}
Human(String f,String l)//Parameterised
Constructor
{
first_name=f;
last_name=l;
}
void getName()
{
System.out.println("first_name
:"+first_name);
System.out.println("last_name
:"+last_name);
}
}
class Student extends Human
{
private int mark;
Student()
{
super();//calls Default
Constructor
mark=0;
}
Student(String f,String l,int m)//here we pass
argument first to parent class and last for this class
{
super(f,l);//calls Parameterised
Constructor
mark=m;
}
}
class Worker extends Human
{
private int wages, hours;
Worker()
{
super();
wages=0;
hours=0;
}
void setWage(int w,int h)
{
wages=w;
hours=h;
}
void calwages()
{
int sal=wages*hours;
System.out.println("Salary
:"+sal);
}
}
class HumanWages
{
public static void main(String args[])
{
Human h1=new Human();
Human h2=new
Human("Shan","Naik");
h2.getName();
Student s=new
Student("Vish","Sharma",100);
s.getName();
Worker w =new Worker();
w.getName();
w.setWage(1000,10);
w.calwages();
}
}