abstract classes and casting objects

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

Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Object Oriented Concepts Classes II. Object Oriented concepts Encapsulation Composition Inheritance Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Class Relationships A class defines a type of data Composition allows an object of another class to define an attribute of a class –Employee “has a”
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Object Oriented programming Instructor: Dr. Essam H. Houssein.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance ndex.html ndex.htmland “Java.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Object-Oriented Programming: Polymorphism Chapter 10.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 5:Interfaces and Abstract Classes
Object-Oriented Concepts
Object-Oriented Programming: Polymorphism
Chapter 15 Abstract Classes and Interfaces
Chapter 12 OOP: Polymorphism, Interfaces and Operator Overloading
Object-oriented Programming in Java
Chapter 11 Inheritance and Polymorphism
Interface, Subclass, and Abstract Class Review
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Object-Oriented Programming: Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Class vs Abstract vs Interface
Chapter 13 Abstract Classes and Interfaces
Object Oriented Programming
Lecture 23 Polymorphism Richard Gesick.
Chapter 9 Inheritance and Polymorphism
Chapter 19 Generics Dr. Clincy - Lecture.
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Based on slides from Deitel & Associates, Inc.
Chapter 9: Polymorphism and Inheritance
Polymorphism, Abstract Classes & Interfaces
Java – Inheritance.
Java Inheritance.
Advanced Inheritance Concepts
Object-Oriented Programming: Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 13 Abstract Classes and Interfaces Part 01
Presentation transcript:

abstract classes and casting objects Professor Evan Korth New York University

Road map Casting objects instanceof Keyword: abstract Abstract methods Abstract classes

review What does it mean to have a protected data member? What does Object’s default equals() method test? What does Object’s default toString() method return?

Given the following class headings: Review (cont) Given the following class headings: public class Ship public class FishingBoat extends Ship public class SpeedBoat extends Ship Which of the following are legal? Ship s = new Ship(); Ship s = new FishingBoat(); FishingBoat f = new Ship(); FishingBoat f = new FishingBoat();

Casting objects (review) Class variables can refer to objects of related classes as long the object has an “is a” relationship to the class. Since subclasses have an ‘is-a’ relationship with their superclass, we can always point a superclass reference variable to a subclass object. In such case a casting operator is not necessary. You implicitly cast the object to it’s superclass just by using the gets operator in an assignment statement. For example: Superclass sup = new Subclass(); Or Subclass subclassVar = new Subclass(); Superclass sup = subclassVar;

Casting objects (review) On the other hand, an object referenced by a superclass variable may or may not be a subclass object. To attempt an assignment such as: Subclass sub = superclassVar; You must explicitly cast the superclass object to the subclass type. For example: Subclass sub = (Subclass) supclVar; Note: If the variable supclVar in the example above is not an instance of Subclass, a runtime error will occur.

Casting objects (cont) In order to help prevent the type of runtime error described in the last slide, Java has the keyword instanceof, which is used to test a variable to see if it is an instance of a class. For example: (supclVar instanceof Subclass) will evaluate to true if and only if supclVar references an instance of class Subclass. So we can test a variable to see if it is an instance of a class before we make an explicit cast. For example: if (supclVar instanceof Subclass) Subclass sub = (Subclass) supclVar; Will never cause a runtime error because the assignment will only happen in the event that supclVar is an instance of Subclass.

Liang’s example If we create a GeometricObject variable, we can “point” it to a Rectangle or a Circle. We can access either Circle’s toString() method or Rectangle’s toString() method through the reference variable. What if we wanted to access one of their getArea() methods?

Abstract Classes and Methods Are superclasses (called abstract superclasses) Cannot be instantiated Incomplete subclasses fill in "missing pieces" Concrete classes Can be instantiated Implement every method they declare Provide specifics

Abstract Classes and Methods (Cont.) Abstract classes not required, but reduce client code dependencies To make a class abstract Declare with keyword abstract Contain one or more abstract methods public abstract double getArea(); Abstract methods No implementation, must be overridden

10.7 Case Study: Payroll System Using Polymorphism Create a payroll program Use abstract methods and polymorphism Problem statement 4 types of employees, paid weekly Salaried (fixed salary, no matter the hours) Hourly (overtime [>40 hours] pays time and a half) Commission (paid percentage of sales) Base-plus-commission (base salary + percentage of sales) Boss wants to raise pay by 10%

10.9 Case Study: Payroll System Using Polymorphism Superclass Employee Abstract method earnings (returns pay) abstract because need to know employee type Cannot calculate for generic employee Other classes extend Employee Employee SalariedEmployee HourlyEmployee CommissionEmployee BasePlusCommissionEmployee

NOTE An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract. In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass. Copyright: Liang

NOTE A subclass can be abstract even if its superclass is concrete. For example, in the book, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract. Copyright: Liang

NOTE You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of GeometricObject type which is an abstract class, is correct (see book). GeometricObject[] geo = new GeometricObject[10]; Copyright: Liang

Dynamic binding The Java Virtual Machine is responsible for binding the correct method to the method call at run time.