Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners How to implement a superclass Person?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


zam
How to implement a superclass Person?
1 Answer(s)      4 years and 8 months ago
Posted in : Java Beginners


How to implement a superclass Person. Make two classes, Student and Lecturer, that inherit from Person. A person has a name and year of birth. A student has a degree program and a lecturer has a salary. Write the class definitions for all classes. Each class must have a constructor and a toString() method that prints the related attributes. Also, write a program name TestQ2 that instantiates an object of each of these classes and invokes the toString() method of each of these object.

how to implement the class..and how to write the toString method?
please help me...

Advertisement
View Answers

January 30, 2010 at 2:35 PM


Hi Friend,

Try the following code:

class Person{
int year;
String name;
public Person(String name,int year){
this.year=year;
this.name=name;
}
public String toString() {
return "Name= " + name + ", Year= " +year;
}
}
class Student extends Person{
private String degree;
public Student(String name,int year,String degree){
super(name,year);
this.degree=degree;
}
public String toString() {
super.toString();
return "Name= "+super.name+", Year= "+super.year+", Degree= " +degree;
}

}
class Lecturer extends Person{
private int salary;
public Lecturer(String name,int year,int salary){
super(name,year);
this.salary=salary;
}
public String toString() {
return "Name= "+super.name+ ", Year= "+super.year+ ", Salary= " +salary;
}
}
public class TestQ2
{
public static void main(String[] args)
{
Person p = new Person("Angel", 1959);
Student s = new Student("Ane", 1979, "MCA");
Lecturer e = new Lecturer("Aurther", 1969, 65000);
System.out.println(p);
System.out.println(s);
System.out.println(e);

}
}

Thanks


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com