Presentation on theme: "Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)"— Presentation transcript:
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Agenda Administrivia Inheritance Polymorphism Interfaces Abstract Classes School of Information Systems & Technology2
Administrivia My name - Imroz Khan My email - email@example.com My AIM handle - imr 0 zkhan My office hours: Posted in the syllabus Any other time via appointment School of Information Systems & Technology3
Inheritance 4 Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities Subclass extends superclass Subclass More specialized group of objects Behaviors inherited from superclass Can customize Additional behaviors
Inheritance 5 Class hierarchy Direct superclass Inherited explicitly (one level up hierarchy) Indirect superclass Inherited two or more levels up hierarchy Single inheritance Inherits from one superclass Multiple inheritance Inherits from multiple superclasses Java does not support multiple inheritance
Inheritance 6 Superclasses and subclasses Object of one class “is an” object of another class Example: Rectangle is quadrilateral. Class Rectangle inherits from class Quadrilateral Quadrilateral : superclass Rectangle : subclass Superclass typically represents larger set of objects than subclasses Example: superclass: Vehicle Cars, trucks, boats, bicycles, … subclass: Car Smaller, more-specific subset of vehicles
Inheritance 7 Inheritance hierarchy Inheritance relationships: tree-like hierarchy structure Each class becomes superclass Supply members to other classes OR subclass Inherit members from other classes
Inheritance 8 Inheritance hierarchy Inheritance relationships: tree-like hierarchy structure Each class becomes superclass Supply members to other classes OR subclass Inherit members from other classes 8 Inheritance hierarchy for university CommunityMembers
Relationship between Superclasses and Subclasses 9 Superclass and subclass relationship Example: CommissionEmployee/BasePlusCommissionEmployee inheritance hierarchy CommissionEmployee First name, last name, SSN, commission rate, gross sale amount BasePlusCommissionEmployee First name, last name, SSN, commission rate, gross sale amount Base salary
Creating and Using a CommissionEmployee Class 10 Class CommissionEmployee Extends class Object Keyword extends Every class in Java extends an existing class Except Object Every class inherits Object ’s methods New class implicitly extends Object If it does not extend another class
Creating and Using a CommissionEmployee Class 11 Class CommissionEmployee Extends class Object Keyword extends Every class in Java extends an existing class Except Object Every class inherits Object ’s methods New class implicitly extends Object If it does not extend another class The Java compiler sets the superclass of a class to Object when the class declaration does not explicitly extend a superclass.
Creating a Inheritance Hierarchy 15 Class BasePlusCommissionEmployee2 Extends class CommissionEmployee Is a CommissionEmployee Has instance variable baseSalary Inherits public and protected members Constructor not inherited
Constructors in Subclasses 19 Instantiating subclass object Chain of constructor calls subclass constructor invokes superclass constructor Implicitly or explicitly Base of inheritance hierarchy Last constructor called in chain is Object ’s constructor Original subclass constructor’s body finishes executing last Example: CommissionEmployee3- BasePlusCommissionEmployee4 hierarchy CommissionEmployee3 constructor called second last (last is Object constructor) CommissionEmployee3 constructor’s body finishes execution second (first is Object constructor’s body)
Constructors in Subclasses 20 When a program creates a subclass object, the subclass constructor immediately calls the superclass constructor (explicitly, via super, or implicitly). The superclass constructor’s body executes to initialize the superclass’s instance variables that are part of the subclass object, then the subclass constructor’s body executes to initialize the subclass-only instance variables.(cont…) Java ensures that even if a constructor does not assign a value to an instance variable, the variable is still initialized to its default value (e.g., 0 for primitive numeric types, false for boolean s, null for references).
Software Engineering with Inheritance 21 Customizing existing software Inherit from existing classes Include additional members Redefine superclass members No direct access to superclass’s source code Link to object code Independent software vendors (ISVs) Develop proprietary code for sale/license Available in object-code format Users derive new classes Without accessing ISV proprietary source code
Polymorphism 22 Polymorphism Enables “programming in the general” The same invocation can produce “many forms” of results Interfaces Implemented by classes to assign common functionality to possibly unrelated classes
Polymorphic Behavior 23 A superclass reference can be aimed at a subclass object This is possible because a subclass object is a superclass object as well When invoking a method from that reference, the type of the actual referenced object, not the type of the reference, determines which method is called A subclass reference can be aimed at a superclass object only if the object is downcasted
Abstract Classes and Methods 24 Abstract classes Classes that are too general to create real objects Used only as abstract superclasses for concrete subclasses and to declare reference variables Many inheritance hierarchies have abstract superclasses occupying the top few levels Keyword abstract Use to declare a class abstract Also use to declare a method abstract Abstract classes normally contain one or more abstract methods All concrete subclasses must override all inherited abstract methods
final Methods and Classes 25 final methods Cannot be overridden in a subclass private and static methods are implicitly final final methods are resolved at compile time, this is known as static binding Compilers can optimize by inlining the code final classes Cannot be extended by a subclass All methods in a final class are implicitly final
26 Assignment Write a class named Coin that models a coin. The class should include the following: A data member that stores the side of the coin facing up: either heads or tails. A constructor that takes no arguments and calls on the flip method (below) to determine which side is facing up. A method named flip, that uses a random number generator to determine which side is facing up. A method named isHeads, that returns true or false, depending on whether the coin is heads up or not. A ToString method which returns a String telling which side of the coin is facing up. When you finish writing the Coin class, next write a subclass of Coin named MonetaryCoin. This class should have the following: A data member that stores the value associated with the coin. A method that returns the value of this data member. A constructor that takes one double argument which represents the value of the coin and then calls the constructor from the superclass. A ToString method which returns a String telling the value of the coin and which side is facing up. Note that this method should be calling the ToString method from the superclass. Finally, write a driver program that prompts the user for the number of pennies, nickels, dimes and quarters to create. The program should then create the appropriate number of coins and display the following: The total value of all coins created The total value of all coins that are heads up The total value of all coins that are tails up Continue to prompt the user to see if they want to flip all of the coins. If they choose yes, flip all coins and then display the value outputs again, otherwise end the program. When you are finished, submit all of your Java source code files to be graded.