Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Java Inheritance & Polymorphism: Super, Overriding, & toString() in Students, Slides of Network security

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

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

106 documents

1 / 41

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objectives
the use of super, overriding, method
polymorphism (dynamic binding),
protected access, toString()
9. More on Inheritance
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download Understanding Java Inheritance & Polymorphism: Super, Overriding, & toString() in Students and more Slides Network security in PDF only on Docsity!

Objectives

  • the use of super, overriding, method

polymorphism (dynamic binding),

protected access, toString()

  1. More on Inheritance

Topics

• 1. Students Example

• 2. Method Polymorphism

• 3. Extends and Private

• 4. DoME v.2 Output Problem

• 5. The Object Class's Methods

Student.java

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

TestStuds.java

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

Compilation and Execution

$ javac Student.java

$ javac GradStudent.java

$ javac TestStuds.java

$ java TestStuds

TestStuds Output

$ 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

Method Lookup for s1.toString()

Student s

Student object

instance of

Method Lookup for gs1.toString()

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

TestStuds.java Continued

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

Output

Student stud Grad Student: John Smith, 200, 4, Pharmacy, Retail Thesis Year 4

o Even though stud is of type Student, it

can refer to a GradStudent object

– because GradStudent is a subclass of

Student

Method Lookup of stud.toString()

Overriding again: use the first version found.

Student stud

GradStudent object

instance of