OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CS 116 Object Oriented Programming II Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance.
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Final and Abstract Classes
Inheritance and Polymorphism
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Computing with C# and the .NET Framework
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Lecture 14 - Abstract Classes
Can perform actions and provide communication
One class is an extension of another.
Chapter 9 Inheritance and Polymorphism
Can perform actions and provide communication
Corresponds with Chapter 7
Comp 249 Programming Methodology
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Java – Inheritance.
Can perform actions and provide communication
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chengyu Sun California State University, Los Angeles
Presentation transcript:

OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS Copyright: 2017 Illinois Institute of Technology/George Koutsogiannakis

Last week’s topics Inheritance Concepts Inheritance Design Inherited Members of a Class Subclass Constructors Overriding Inherited Methods

This week ‘s Topics Adding Specialization to the Subclass Abstract Classes and Methods

The BankAccount Heirarchy Note 1: + indicates public and – indicates private Note 2: return types Are shown after the Name and arguments listing of a method

Adding Specialization A subclass can define new fields and methods. Our CheckingAccount class adds these instance variables: monthlyFee, a double DEFAULT_FEE, a double constant these methods: setMonthlyFee, the accessor getMonthlyFee, the mutator applyMonthlyFee, which charges the monthly fee to the account

The applyMonthlyFee Method Because balance is private in the BankAccount class, the applyMonthlyFee method calls the withdraw method to subtract the monthly fee from the balance: public void applyMonthlyFee( ) { withdraw( monthlyFee ); } Why the indirect call and not a direct call to balance? See Examples 10.7 and 10.8 in text

SOFTWARE ENGINEERING TIP The superclasses in a class hierarchy should define fields and methods common to all subclasses. The subclasses should add specialized fields and methods. That is called specialization

Overriding Inherited Methods A subclass can override (or replace) an inherited method by providing a new version of the method. The API of the new version must match the inherited method. When the client calls the method, it will call the subclass version. The superclass method is invisible to the client of the subclass, but the subclass methods can still call the superclass method using this syntax: super.methodName( argument list )

The toString Method The toString method in the CheckingAccount class overrides the toString method in the BankAccount class. The subclass version calls the superclass version to return balance. public String toString( ) { return super.toString( ) + "; monthly fee is " + MONEY.format( monthlyFee ); } See Examples 10.9 & 10.10 in text

Inheritance Rules for Overridden Methods Superclass Members Inherited by subclass? Directly Accessible by Subclass? Directly Accessible by Client of Subclass Using a Subclass Reference? public or protected inherited methods that have been overridden in the subclass No, because the subclass has a new version of these methods yes, using super.methodName( arg list ) No, the client classes of the subclass can only call the overridden versions. They could, however, call the superclass versions if the overridden version uses super.methodName( To call the original method

Common Error Trap Do not confuse overriding a method with overloading a method. Overriding a method: A subclass provides a new version of that method (same signature), which hides the superclass version from the client. Overloading a method: A class provides a version of the method, which varies in the number and/or type of parameters (different signature). A client of the class can call any of the public versions of overloaded methods.

The protected Access Modifier Declaring fields as private preserves encapsulation. Subclass methods call superclass methods to set the values of the fields, and the superclass methods enforce the validation rules for the data. But calling methods incurs processing overhead. Declaring fields as protected allows them to be accessed directly by subclass methods. Classes outside the hierarchy and package must use accessors and mutators for protected fields.

abstract Classes and Methods An abstract class is a class that is not completely implemented. Usually, the abstract class contains at least one abstract method. An abstract method specifies an API but does not provide an implementation. The abstract method is used as a pattern for a method the subclasses should implement.

More on abstract Classes An object reference to an abstract class can be declared. (Notice the word reference and not instantiation) We use this capability in polymorphism, which will be discussed later. An abstract class cannot be used to instantiate objects (because the class is not complete). An abstract class can be extended. subclasses can complete the implementation and objects of those subclasses can be instantiated

Defining an abstract class To declare a class as abstract, include the abstract keyword in the class header: accessModifier abstract class ClassName { // class body }

Defining an abstract Method To declare a method as abstract, include the abstract keyword in the method header, and end the header with a semicolon: accessModifier abstract returnType methodName( argument list ); Note: The semicolon at the end of the header indicates that the method has no code. We do not use opening and closing curly braces { }.

Example Hierarchy We can define a Figure hierarchy. The superclass is Figure, which is abstract. (In the UML diagram, Figure is set in italics to indicate that it is abstract.) We will derive two subclasses: Circle and Square.

The Figure Hierarchy in UML

Subclasses of abstract Classes A subclass of an abstract class can implement all, some, or none of the abstract methods. If the subclass does not implement all of the abstract methods, it must also be declared as abstract. Our Circle subclass adds a radius instance variable and implements the draw method. Our Square subclass adds a length instance variable and implements the draw method. See Examples 10.15, 10.16, 10.17, & 10.18 in text

Restrictions for Defining abstract Classes Classes must be declared abstract if the class contains any abstract methods abstract classes can be extended An object reference to an abstract class can be declared abstract classes cannot be used to instantiate objects

Restrictions for Defining abstract Methods abstract methods can be declared only within an abstract class An abstract method must consist of a method header followed by a semicolon abstract methods cannot be called abstract methods cannot be declared as private or static A constructor cannot be declared abstract

The Figure Class public abstract class Figure { private int x; private int y; private Color color; // usual constructors, accessors, // and mutators // abstract draw method public abstract void draw( Graphics g ); } All classes in the hierarchy will have an (x, y) coordinate and color. Subclasses will implement the draw method.

Polymorphism The fact that the draw method is implemented in tow different ways (by Circle and Square classes) is an example of polymorphism Polymorphism means “many forms” or “many manifestations” in Greek. Our method draw has many manifestations since one rime we use it to draw a circle and another time we use it to draw a square. The client class will know how to call the correct version.

Study Guide Chapter 10 Section 10.1 TO 10.5