1 / 32

Object-Oriented Application Development (Part II)

Object-Oriented Application Development (Part II). Dr.Wanwipa Titthasiri Department of Computer Science. Objectives. In this chapter you will: Study the concepts of inheritance and polymorphism Learn more about how Java’s predefined classes use inheritance and polymorphism

ronli
Download Presentation

Object-Oriented Application Development (Part II)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Application Development (Part II) Dr.Wanwipa Titthasiri Department of Computer Science

  2. Objectives In this chapter you will: • Study the concepts of inheritance and polymorphism • Learn more about how Java’s predefined classes use inheritance and polymorphism • Explore how inheritance and polymorphism are used in programmer-defined classes • Learn the importance of overloading, overriding, and dynamic binding • Develop skills in using inheritance and polymorphism in object-oriented applications An Introduction to Java Programming and Object-Oriented Application Development

  3. Inheritance • Inheritance is a relationship in which one class has access to public members of another • Java has predefined classes that inherit attributes and behaviors from other classes • Programmer-defined classes can inherit from predefined or programmer-defined classes An Introduction to Java Programming and Object-Oriented Application Development

  4. Inheritance and Java’s Predefined Classes • Inheritance hierarchy: Classes farther down inherit from classes farther up • Superclass: A class that is at a higher level • Subclass: A class at a lower level • Direct superclass: A class immediately above a given class • Direct subclass: A class immediately below a given class An Introduction to Java Programming and Object-Oriented Application Development

  5. Inheritance and Java’s Predefined Classes (continued) An Introduction to Java Programming and Object-Oriented Application Development

  6. Inheritance and Java’s Predefined Classes (continued) • Subclasses have access to public and protected members of their superclasses • Is-a relationship: A subclass has an “is-a” relationship with its superclass • Is-a relationships are also called generalization relationships • A superclass is more general that its subclasses • Example: Number is more general than double An Introduction to Java Programming and Object-Oriented Application Development

  7. Inheritance and Programmer-Defined Classes • Recall MusicWorldApp.java • CDOrder has a LineItem and LineItem has a CD object • Does not involve inheritance • Inheritance is needed when a general type can be broken down into specific types • Example: The information system in a university must model different types of people • A person is a general type of object • A Person class serves as a superclass An Introduction to Java Programming and Object-Oriented Application Development

  8. Inheritance and Programmer-Defined Classes (continued) An Introduction to Java Programming and Object-Oriented Application Development

  9. Inheritance and Programmer-Defined Classes (continued) • A student is a person, so Student is a subclass of Person • All attributes of Person are attributes of Student • A student is a more specialized instance of a person • Students have a college class (junior, senior etc.) • Faculty and staff do not have a college class • Only methods unique to Student are defined An Introduction to Java Programming and Object-Oriented Application Development

  10. Inheritance and Programmer-Defined Classes (continued) An Introduction to Java Programming and Object-Oriented Application Development

  11. Inheritance and Programmer-Defined Classes (continued) • The keyword extends means inherits from • The class Student inherits public and protected members of Person • The keyword super calls the superclass constructor • When a Student object is created, a Person object is also created • The keyword this refers to the object whose method or constructor is being called An Introduction to Java Programming and Object-Oriented Application Development

  12. Inheritance and Programmer-Defined Classes (continued) • The object-defining classes Person and Student cannot be executed on their own • An application class is required to execute tasks required by the application • Application classes contain a main method • The JVM looks up the inheritance hierarchy for methods if it doesn’t find it in the calling class An Introduction to Java Programming and Object-Oriented Application Development

  13. Inheritance and Programmer-Defined Classes (continued) An Introduction to Java Programming and Object-Oriented Application Development

  14. Inheritance and Programmer-Defined Classes (continued) An Introduction to Java Programming and Object-Oriented Application Development

  15. Apply the Concept • Develop a simple banking application • Checking accounts have a monthly service fee • Savings accounts pay interest monthly • Three tasks • Identify all object-defining classes required • Sketch a class diagram • Plan the application class with a flowchart • Nouns are modeled by objects • Bank account, checking account, savings account • Verbs are modeled with methods • Assess fee, pay interest An Introduction to Java Programming and Object-Oriented Application Development

  16. Apply the Concept (continued) • Create a class diagram • Focus on customer, bank account, checking account, and savings account • Checking and savings accounts are specialized bank accounts • Class name in the top section, instance variables in the middle, methods in the bottom • Composition is represented by solid diamonds and inheritance is represented by open arrows • Application class is represented with a flowchart An Introduction to Java Programming and Object-Oriented Application Development

  17. Apply the Concept (continued) An Introduction to Java Programming and Object-Oriented Application Development

  18. Apply the Concept (continued) • Customer.java defines Customer objects • SSN, name, checking account, savings account • Constructor, accessor methods for accounts • BankAccount.java defines BankAccount objects, members common to all accounts • Account number, balance, accessor methods, toString • CheckingAccount.java and SavingsAccount.java nearly identical • Checking accounts incur a service charge, savings accrue interest An Introduction to Java Programming and Object-Oriented Application Development

  19. Apply the Concept (continued) • Both CheckingAccount and SavingsAccount class definitions include extendsBankAccount • Constructors call the BankAccount constructor first using the keyword super • BankingApp.java is procedural with 5 parts • Preparation, declaration, input, processing, output • All individual classes must be in the same directory, and be compiled individually An Introduction to Java Programming and Object-Oriented Application Development

  20. Polymorphism • Polymorphism refers to the ability of a single method name to be used in different ways • Different kinds of polymorphism • Overloading • Overriding • Static binding • Dynamic binding An Introduction to Java Programming and Object-Oriented Application Development

  21. Overloading • Overloading occurs when a single method name is used more than once • In the same class • Different parameter lists (different signatures) • Example: The indexOf method in the String class • Parameter lists can differ in number, type or order of arguments An Introduction to Java Programming and Object-Oriented Application Development

  22. Overloading (continued) An Introduction to Java Programming and Object-Oriented Application Development

  23. Overriding • One method name is used in different classes within an inheritance hierarchy (same signature) • Each of the methods may do completely different things • A method in a subclass may override a method in its superclass • Example: The toString method in Object is overridden in the BankAccount class • If a method is not found in the named class, Java looks up the hierarchy An Introduction to Java Programming and Object-Oriented Application Development

  24. Static (Early) and Dynamic (Late) Binding • When overriding is used, the Java compiler attempts to bind a method call to its method definition • Binding at compile time is known as static (early) binding • Dynamic binding means the method call is not bound to the definition until run time An Introduction to Java Programming and Object-Oriented Application Development

  25. Static (Early) and Dynamic (Late) Binding (continued) • Step 1: ObjectA is created from ClassA • Step 2: ObjectB is created from ClassB • Assume ClassB is a subclass of ClassA • ClassA contains a method called methodA • ObjectA.methodA() the compiler checks ObjectA for methodA: static binding • Step 3: ObjectB=ObjectA • Java thinks ObjectA is an instance of ClassB • Step 4: ObjectA tries to call methodA • Java does not check ClassB for methodA An Introduction to Java Programming and Object-Oriented Application Development

  26. Static (Early) and Dynamic (Late) Binding (continued) An Introduction to Java Programming and Object-Oriented Application Development

  27. Apply the Concept • Recall that a bank offers checking (with a service charge) and savings (with interest) • For simplicity, three classes • PolySavingsAccount is a subclass of PolyBankAccount • PolyBankingApp is a driver • PolyBankAccount has a payInterest method but cannot pay interest An Introduction to Java Programming and Object-Oriented Application Development

  28. Apply the Concept (continued) An Introduction to Java Programming and Object-Oriented Application Development

  29. Apply the Concept (continued) An Introduction to Java Programming and Object-Oriented Application Development

  30. Apply the Concept (continued) • PolyBankAccount has overloaded constructors • The method payInterest is called on an instance of PolySavingsAccount • Overrides payInterest in PolyBankAccount • Dynamic binding (Lines 24 – 29) • account3 created from PolyBankAccount • account3 now refers to PolySavingsAccount • account3 calls methods from PolySavingsAccount • The payInterest method is not bound to account3 until run time An Introduction to Java Programming and Object-Oriented Application Development

  31. Apply the Concept (continued) An Introduction to Java Programming and Object-Oriented Application Development

  32. Summary • A superclass is higher in the hierarchy and a subclass is lower • The inheritance relationship is an is-a relationship or generalization relationship • A subclass inherits public and protected members of its superclass • Polymorphism means a single method can have many implementations depending on its signature and location • Overloading and overriding (with static and early binding) are forms of polymorphism An Introduction to Java Programming and Object-Oriented Application Development

More Related