

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An in-depth exploration of java inheritance and polymorphism concepts through the use of a students example. Topics include method polymorphism, extends and private, and the object class's methods. Students will learn about super and overriding, method lookup, and super calls in methods.
Typology: Slides
1 / 41
This page cannot be seen from the preview
Don't miss anything!
public class Student { private int student_id; private int year; private String name;
public Student(String nm, int id, int y)
{ name = new String(nm); student_id = id; year = y; }
continued Docsity.com
public String toString() { return "Student: " + name + ", " + student_id + ", " + year; }
public int year_group() { return year; }
} // end of Student class
public String toString() { return "Grad " + super. toString() + ", " + dept + ", " + thesis; }
} // end of GradStudent class
public class TestStuds
{
public static void main(String args[]) { Student s1 = new Student("Jane Doe", 100 , 1 );
GradStudent gs1; gs1 = new GradStudent("John Smith", 200 , 4, "Pharmacy", "Retail Thesis"); :
continued Docsity.com
$ javac Student.java
$ javac GradStudent.java
$ javac TestStuds.java
$ java TestStuds
$ java TestStuds
Student s
Student: Jane Doe, 100 , 1
Year 1
Grad student gs
GradStudent: John Smith, 200 , 4, Pharmacy,
Retail Thesis
Year 4
:
// see later
Student s
Student object
instance of
Overriding : use the first version of toString() found in the inheritance hierarchy.
GradStudent gs
GradStudent object
instance of
Method Lookup for gs1.year_group()
Move up the inheritance hierarchy until a suitable method is found.
GradStudent gs
GradStudent object
instance of
Student stud; stud = gs1; // refer to subclass
System.out.println("Student stud"); System.out.println( stud.toString () ); System.out.println("Year " + stud.year_group()); }
} // end of TestStuds class
Student stud Grad Student: John Smith, 200, 4, Pharmacy, Retail Thesis Year 4
Student
Overriding again: use the first version found.
Student stud
GradStudent object
instance of