Download presentation
Presentation is loading. Please wait.
Published byVeronika Yuwono Modified over 6 years ago
1
COMPUTER 2430 Object Oriented Programming and Data Structures I
2
Inheritance Object Subclasses inherit all members
from its superclass, except constructors. An instance of a subclass is an instance of the superclass. Subclasses can override the methods inherited from the superclass. Person Student
3
Syntax at Compiling Time
A variable of a superclass can point to instances of its subclasses, but a variable of a subclass can not point to an instance of its superclass (unless it’s an instance of the subclass). On a variable of the superclass, any methods of the superclass can be invoked, but not additional methods of its subclass, even the instance pointed at is an instance of the subclass.
4
public class CS2430Sx { public static void main(String[] args) Object obj = new Object(); Person person = new Person("Rocky"); Student student = new Student("Frank"); // Valid? student = person; person = obj; student = obj; }
5
public class CS2430Sx { public static void main(String[] args) Object obj = new Object(); Person person = new Person("Rocky"); Student student = new Student("Frank"); // Valid? obj = person; person.setName("Frank"); obj.setName("Frank"); }
6
public class CS2430Sx { public static void main(String[] args) Object obj = new Object(); Person person = new Person("Rocky"); Student student = new Student("Frank"); // Valid? person = student; student.setGPA(3.5f); person.setGPA(3.5f); }
7
Method toString() is overridden by Person then by Student:
public class CS2430Sx { public static void main(String[] args) Object obj = new Object(); Person person = new Person("Rocky"); Student student = new Student("Frank"); System.out.println(obj.toString()); System.out.println(person.toString()); System.out.println(student.toString()); } Method toString() is overridden by Person then by Student: Person Rocky Student Frank: GPA 3.0
8
public class CS2430Sx { public static void main(String[] args) Object obj = new Object(); Person person = new Person("Rocky"); Student student = new Student("Frank"); // Valid? obj = person; System.out.println(obj.toString()); // Which version of toString() is called? // Object // Person }
9
Run Time Binding When an overridden method is called on a variable of the superclass, the version of the method that will be called is determined by the instance, not the variable. Object Oriented Programming! It’s very difficult, or impossible in some cases, to determine the class of the instance at the compiling time.
10
public class CS2430Sx { public static void main(String[] args) Object obj; . . . if (cond1) obj = new Person("Rocky"); else if (cond2) obj = new Student("Frank"); else obj = new Object(); System.out.println(obj.toString()); // What is the class of the instance // pointed at by obj? }
11
public class CS2430Sx { public static void main(String[] args) Object obj; . . . doWork(obj); // Is obj a Person or a Student? } private static void doWork( Object obj ) // Which version of toString()? System.out.println(obj.toString()); obj.setGPA(3.5F); // Valid? obj.setName(“Qi”); // Valid?
12
Features of OOP Abstraction Data Encapsulation Data Hiding Inheritance
Polymorphism
13
Polymorphism The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class. Java Document at
14
public class CS2430Sx { public static void main(String[] args) Person person; . . . doWork(person); } private static void doWork( Person person ) // Not Polymoyphism! if (person instanceof Student) (Student)person.doProg2(); else if (person instanceof Professor) (Professor)person.gradeProg2();
15
public class CS2430Sx { public static void main(String[] args) Person person; . . . doWork(person); } private void doWork( Person person ) // Polymoyphism! person.doIt(); // Superclass Person defines method doIt() // Subclass Student and Professor override doIt()
16
private void doWork( Person person )
{ // Polymoyphism! person.doIt(); // Superclass Person defines method doIt() // Subclass Student and Professor override doIt() . . . } private static void doWork( Person person ) // Not Polymoyphism! if (person instanceof Student) (Student)person.doProg2(); else if (person instanceof Professor) (Professor)person.gradeProg2();
17
public class ObjectsList
{ // The array can have instances of any classes private Object[] objList = new Object[1000]; private inr numObjects = 0; . . . private int find( Object target ) // Polymoyphism! // objList[i] could be any classes and // the class has overridden method equals(). for (int i = 0; i < numObjects; i ++) if (objList[i].equals(target)) return i; return -1; }
18
public class ObjectsList
{ private Object[] objList = new Object[1000]; private inr numObjects = 0; . . . private int find( Object target ) // Not Polymoyphism! for (int i = 0; i < numObjects; i ++) if (objList[i] instanceof Student) // same name and same GPA else if (objList[i] instanceof Professor) // same name and same research area else return -1; }
19
Polymorphism The superclass specifies common behaviors.
The subclasses override superclass methods. The actual parameter of a method could be instances of different subclasses, but only the superclass methods are called within the method. private void doWork( Person person ) An array of the superclass could have instances of the subclasses, but only the superclass methods are called when processing the array elements. private Object[] objList = new Object[MAX];
20
Features of OOP Abstraction Data Encapsulation Data Hiding Inheritance
Polymorphism Better maintainability Better extensibility
21
public class PersonsList
{ private Person[] allPersons = new Person[1000]; private int personsCount = 0; . . . private int find( Person target ) for (int i = 0; i < numObjects; i ++) if (allPersons[i] instanceof Student) // same name and same GPA else if (allPersons[i] instanceof Professor) // same name and same research area else return -1; }
22
/** Needs to be modified when adding new subclasses removing existing subclasses modifying existing subclasses Not Polymorphism */ private int find( Person target ) { for (int i = 0; i < numObjects; i ++) if (objList[i] instanceof Student) // same name and same GPA else if (objList[i] instanceof Professor) // same name and same research area else . . . return -1; }
23
/** Remain the same when adding new subclasses removing existing subclasses modifying existing subclasses Subclasses override equals in different ways! Polymorphism! */ private int find( Person target ) { for (int i = 0; i < numObjects; i ++) if (allPersons[i].equals(target)) return i; return -1; }
24
Prog2 Polymorphism! Superclass Subclasses GolfLeagueMember
RegularMember SeniorMember YouthMember
25
// No variables of any subclasses!
public class GolfLeague { private final int MAX_SIZE = 10; private GolfLeagueMember members[] = new GolfLeagueMember[MAX_SIZE]; private int numOfMembers = 0; public boolean add(GolfLeagueMember member) public boolean delete(String name) public boolean enterScore (String name, int score) public int getHandicap(String name) @Override public String toString() . . . }
26
// No variables of any subclasses!
public class Prog2 { private static GolfLeague league = new GolfLeague(); private static Scanner sc; public static void main( String[] args) // Need to be modified when adding new subclasses // or removing existing subclasses private static void addMember() private static void deleteMember() private static void addScore() private static void displayHandicap() . . . }
27
Abstract Methods Other common methods for Student and Professor?
Have Parties! Different ways to have parties Is it possible to specify it in superclass Person?
28
public class Person { private String name, address, phone, ; private Date dob; public boolean setName (String s) public String getName() /** Subclasses Student and Professor have different ways to have parties. Polymorphism? */ public void haveParty () if (this instanceof Student) . . . else if (this instanceof Professor) else }
29
public class Person { private String name, address, phone, ; private Date dob; public boolean setName (String s) public String getName() /** Subclasses Student and Professor have different ways to have parties and will implement it inside their own subclasses. Polymorphism! */ public abstract void haveParty (); // Abstract method: No method body at all! }
30
/** Class must be abstract if it has one abstract method. */ public abstract class Person { private String name, address, phone, ; private Date dob; public boolean setName (String s) public String getName() Subclasses Student and Professor have different ways to have parties and will implement it inside their own subclasses. public abstract void haveParty (); }
31
public abstract class Person
{ . . . abstract public void haveParty (); } public class CS2430Sx public static void main(String[] args) // Valid? Person person = new Person(); // NO! // No body for method haveParty()! person.haveParty();
32
public abstract class Person
{ . . . abstract public void haveParty (); } public class CS2430Sx public static void main(String[] args) // Valid? Person person = new Student(); // Yes! // Subclass must override method haveParty()! person.haveParty();
33
public abstract class GolfLeagueMember
{ . . . public abstract int handicap(); } public class RegularMember extends GolfLeagueMember @Override public int handicap() public class SeniorMember extends GolfLeagueMember public class YouthMember extends GolfLeagueMember
34
// No variables of any subclasses!
public class GolfLeague { private final int MAX_SIZE = 10; private GolfLeagueMember members[] = new GolfLeagueMember[MAX_SIZE]; private int numOfMembers = 0; . . . public int getHandicap(String name) // find the index for the golf member with given name // each element is an instance of GolfLeagueMember return members[index].handicap(); }
35
Schedule Lab 3 Class in Lab 206 on Wednesday Bring your Prog1
Due Monday, October 1 Quiz 2 Friday, September 28 Prog2 Due Friday, October 5 Test 1 Wednesday, October 10
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.