Expected Understanding: Access Modifiers, Single Inheritance, getter methods, Constructor Overloading
1) Create a Class named “Trainer”.
– Have default instance variables String dept, institute
– Have private instance variable int salary
– Assign 10000 as value for salary.
– Create getter method for salary.
– Assign values – “Java”, “Payilagam” to them
– Have instance method training() with void as return data type
– Add a print statement inside training() method
– Have instance named as ‘trainerKumar’ and pass “CSE”, “payilagam” as arguments to it.
– Handle above line with matching Constructor.
class Trainer
{
String dept,institute;
private int salary =10000;
public Trainer()
{
System.out.println("dept");
System.out.println("institute");
}
public Trainer(String dept,String institute)
{
this.dept=dept;
this.institute=institute;
System.out.println("The dept is " +this.dept + " The institute is " +this.institute);
}
public void training(String dept, String institute)
{
System.out.println("The dept is " +dept + " The institute is " +institute);
}
public int getsalary()
{
return this.salary=salary;
}
public static void main(String[] args)
{
Trainer trainerkumar = new Trainer("Java", "Payilagam");
trainerkumar.training("CSE", "Payilagam");
}
}
2) Create a sub class “SQLTrainer” under “Trainer”.
– Have main method in it.
– Create instance ram for this class
– Handle with proper super class constructor
– Access parent class instance variables
– Call parent class instance method training()
– Access salary using getter method in parent class
public class SQLTrainer extends Trainer
{
SQLTrainer()
{
super();
}
public static void main(String[] args)
{
SQLTrainer ram = new SQLTrainer();
ram.training("CSE" ,"Payilagam");
System.out.println(ram.getsalary());
}
}
Output
