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

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

Have Programming Question? Ask it here!

How to implement a superclass Person?
1 Answer(s)      4 years and 6 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...

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){
public String toString() {
return "Name= " + name + ", Year= " +year;
class Student extends Person{
private String degree;
public Student(String name,int year,String degree){
public String toString() {
return "Name= "", Year= "+super.year+", Degree= " +degree;

class Lecturer extends Person{
private int salary;
public Lecturer(String name,int year,int salary){
public String toString() {
return "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);



Related Tutorials/Questions & Answers: