350 likes | 368 Views
COMPUTER 2430 Object Oriented Programming and Data Structures I. 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.
E N D
COMPUTER 2430Object Oriented Programming andData Structures I
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
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.
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; // Valid? person = obj; // Valid? student = obj; } }
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; // Valid? person.setName("Frank"); // Valid? obj.setName("Frank"); } }
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; // Valid? student.setGPA(3.5f); // Valid? person.setGPA(3.5f); } }
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: java.lang.Object@15db9742 Person Rocky Student Frank: GPA 3.0
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; // Valid? System.out.println(obj.toString()); // Which version of toString() is called? // Object // Person } }
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.
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? } }
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? } }
Features of OOP • Abstraction • Data Encapsulation • Data Hiding • Inheritance • Polymorphism
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.
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(); . . . } }
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() . . . } }
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(); . . . }
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; } }
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; } }
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];
Features of OOP • Abstraction • Data Encapsulation • Data Hiding • Inheritance • Polymorphism • Bettermaintainability • Better extensibility
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; } }
/** 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; }
/** 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; }
Prog2 • Polymorphism! • Superclass • GolfLeagueMember • Subclasses • RegularMember • SeniorMember • YouthMember
// 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() . . . }
// 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() . . . }
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?
public class Person { private String name, address, phone, email; 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 . . . } }
public class Person { private String name, address, phone, email; 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! }
/** Class must be abstract if it has one abstract method. */ public abstract class Person { private String name, address, phone, email; 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 (); }
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(); } }
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(); } }
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
// 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(); } . . . }
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