/*
3. Write a Teacher class that extends the parent class Person.
a. Add instance variables to the class for subject (e.g. “Computer Science”, “Chemistry”,, “English”, “Other”) and salary (the teachers annual salary). Subject should be of type String and salary of type double. Choose appropriate names for the instance variables.
b. Write a constructor for the Teacher class. The constructor will use five parameters to initialize myName, myAge, myGender, subject, and salary. Use the super reference to use the constructor in the Person superclass to initialize the inherited values.
c. Write “setter” and “getter” methods for all of the class variables. For the Teacher class they would be: getSubject, getSalary, setSubject, and setSalary.
d. Write the toString() method for the Teacher class. Use a super reference to do the things already done by the superclass.
*/
public class MianActivity
{
public static void main(String[] args)
{
Teacher teacher = new Teacher("Bpn", 22, "Male", "JAVA", 2000);
teacher.getName();
System.out.println(teacher.getName()+" "+teacher.getAge()+" "+teacher.getGender()+" "+teacher.getSubject()+" "+teacher.getSalary());
}
}
-----------------------------------------------------------------
package gettersSettersWithConstructors;
public class Person
{
private String name;
private int age;
private String gender;
public Person(String name, int age, String gender)
{
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
}
--------------------------------------------------------------------------
package gettersSettersWithConstructors;
public class Teacher extends Person
{
private double salary;
private String subject;
public Teacher(String name, int age, String gender,String subject, double salary)
{
super(name, age, gender);
this.subject = subject;
this.salary = salary;
}
public double getSalary()
{
return salary;
}
public String getSubject()
{
return subject;
}
public void setSalary(double salary)
{
this.salary = salary;
}
public void setSubject(String subject)
{
this.subject = subject;
}
}

No comments:
Post a Comment