COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Inheritance ndex.html ndex.htmland “Java.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
BY:- TOPS Technologies
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Objects as a programming concept
COMPUTER 2430 Object Oriented Programming and Data Structures I
Interfaces and Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object Oriented Programming
CS 302 Week 11 Jim Williams, PhD.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Extending Classes.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Inheritance.
METHOD OVERRIDING in JAVA
COMPUTER 2430 Object Oriented Programming and Data Structures I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
COMPUTER 2430 Object Oriented Programming and Data Structures I
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Chapter 9 Carrano Chapter 10 Small Java
Advanced Inheritance Concepts
CIS 199 Final Review.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Advanced Programming in Java
CIS 110: Introduction to computer programming
Programming in C# CHAPTER 5 & 6
Presentation transcript:

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. 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; person = obj; 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; person.setName("Frank"); 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; student.setGPA(3.5f); person.setGPA(3.5f); }

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: 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; 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. Java Document at https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html

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 Better maintainability 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 Subclasses GolfLeagueMember 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