How to implement a superclass Person?

How to implement a superclass Person?

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){
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:
How to implement a superclass Person? - Java Beginners
How to implement this superclass Person - Java Beginners
Advertisements
ModuleNotFoundError: No module named 'persons'
ModuleNotFoundError: No module named 'persona-for-igor'
ModuleNotFoundError: No module named 'persona-idp'
ModuleNotFoundError: No module named 'pyramid_persona'
ModuleNotFoundError: No module named 'reddit_persona'
ModuleNotFoundError: No module named 'reddit_persona'
ModuleNotFoundError: No module named 'reddit_persona'
ModuleNotFoundError: No module named 'persona-test-user'
ModuleNotFoundError: No module named 'superClass'
ModuleNotFoundError: No module named 'superClass'
how to implement spring security - Spring
How to implement FTP using java
what is a thread initial state? how to implement it?
Maven dependency for com.jccworld - persona-mock-server version 2.0 is released. Learn to use persona-mock-server version 2.0 in Maven based Java projects
How to implement openId java web based application?
how to implement ajax in struts2 to operate on selectbox in jsp
Version of com.jccworld>persona-mock-server dependency
JAVA CLASSES/ SUPERCLASS
How to implement session variables - JSP-Servlet
what is cloud computing? any how we can implement?
How to implement xml parsing in iphone ? -RV
Maven Repository/Dependency: biz.lobachev.annette | persons-api
how to implement Single Sign On in enterprise application using spring framework
How to implement link in Web app lead to report with print and save features
How to implement Captcha in PHP Form using GD Library
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for persona-mock-server version 1.0
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for persona-mock-server version 1.0
Implement push
Maven Dependency persona-mock-server >> 1.0
How to implement ajax in struts2 to operate on select box in jsp
How can we implement Pop-up in JQuery without Plugin?
Implement push
How can i implement the calculator programe in jsp code
doubt about superclass reference and subclass object
The implement keyword
Implement the Serializable Interface
Implement the Queue in Java
implement microsoft office
implement core data iphone sdk
Implement Drag and drop
Implement an interface in a JSP
Implement transaction serializability
How to Implement ERP?
Where to implement spring - Spring
in order to create jsp and servlet code to add,delete,edit,list of persons in eclipsejavaee
implement sale purchaes
java: implement a class queue
Can an Interface implement another Interface?

Ads