CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Object Oriented Programming
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES LECTURE 1 GEORGE KOUTSOGIANNAKIS George Koutsogiannakis / Summer 2011.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
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.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
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.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
CS 116 Object Oriented Programming II Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Sixth Lecture ArrayList Abstract Class and Interface
Polymorphism, Abstract Classes & Interfaces
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9: Polymorphism and Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism, Abstract Classes & Interfaces
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Inheritance Inheritance is a fundamental Object Oriented concept
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Final and Abstract Classes
CIS 110: Introduction to computer programming
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Last week’ s Topics Adding Specialization to the Subclass Abstract Classes and Methods 2

New Topics Polymorphism – New format for “for loop”. Interfaces Multiple Inheritance 3

Polymorphism An important concept in inheritance is that an object of a subclass is also an object of any of its super classes. That concept is the basis for an important OOP feature, called polymorphism. Polymorphism simplifies the processing of various objects in the same class hierarchy because we can use the same method call for any object in the hierarchy using a super class object reference. 4

Polymorphism Requirements To use polymorphism, these conditions must be true: – the classes are in the same hierarchy – all subclasses override the same method – a subclass object reference is assigned to a superclass object reference – the superclass object reference is used to call the method 5

Example Suppose Figure is abstract and has an abstract method draw. Suppose that Circle and Square inherit from Figure and implement Figure ‘s abstract method draw. 6

Example Suppose we have a class FigureClient. This class uses the previous classes to actually draw figures. i.e. (assuming that all classes are in the same folder) public class FigureClient { public static void main(String [] args) { Circle circle=new Circle(); Square square=new Square(); Figure figure; //just declaration, no instantiation is allowed figure=circle; figure.draw(); // a circle will be drawn figure=square; figure.draw(); // a square will be drawn now. //other code } ……………………} 7

Polymorphism Conditions conditions for polymorphism: – The Figure, Circle, and Square classes are in the same hierarchy. – The non-abstract Circle and Square classes implement the draw method. – We assigned the Circle and Square objects to Figure references. – We called the draw method using Figure references. 8

Example Example shows how we can simplify the drawing of Circle and Square objects. We instantiate a Figure ArrayList and add Circle and Square objects to it. ArrayList figuresList = new ArrayList ( ); figuresList.add( new Square( 150, 100, Color.BLACK, 40 ) ); figuresList.add( new Circle( 160, 110, Color.RED, 10 ) ); … In the paint method, we call draw for each Figure: for ( Figure f : figuresList ) f.draw( g ); //Note: iterates though the list and assigns each list item to the reference f. 9

Reminder of for loop format for ArrayList Assume that we have an ArrayList of VehicleA objects called vehicleArrayList VehicleA va; for(int i=0; i<vehicleArrayList.size(); i++) { va=vehicleArrayList.get(i); } Alternate format: for( VehicleA va: vehicleArrayList) System.out.println(va); /invokes toString //iterates through all the VehicleA objects stored in vehicleArrayList 10

Interfaces A class can inherit directly from only one class, that is, a class can extend only one class. To allow a class to inherit behavior from multiple sources, Java provides the interface. An interface typically specifies behavior that a class will implement. Interface members can be any of the following:  classes  constants  abstract methods  other interfaces 11

Interface Syntax To define an interface, use the following syntax: accessModifier interface InterfaceName { // body of interface } All interfaces are abstract; thus, they cannot be instantiated. The abstract keyword, however, can be omitted in the interface definition. 12

Finer Points of Interfaces An interface's fields are public, static, and final. These keywords can be specified or omitted. When you define a field in an interface, you must assign a value to the field. All methods within an interface must be abstract, so the method definition must consist of only a method header and a semicolon. The abstract keyword also can be omitted from the method definition. 13

Inheriting from an Interface To inherit from an interface, a class declares that it implements the interface in the class definition, using the following syntax: accessModifier class ClassName extends SuperclassName implements Interface1, Interface2, … The extends clause is optional. A class can implement 0, 1, or more interfaces. When a class implements an interface, the class must provide an implementation for each method in the interface (if the implementing class is a concrete class). 14

INTERFACES NOTE: An abstract class can indicate that it implements an interfaces without actually implementing the methods of the interface. – It would be left up to the class that inherits the abstract class to implement the methods of the interface. 15

Example We define an abstract class Animal with one abstract method ( See Example ): public abstract void draw( Graphics g ); We define a Moveable interface with one abstract method: public interface Moveable { int FAST = 5; // static constant int SLOW = 1; // static constant void move( ); // abstract method } 16

Derived Classes TortoiseRacer class – extends Animal class – implements Moveable interface – implements draw and move methods TortoiseNonRacer class – extends Animal class – (does not implement Moveable interface) – implements draw method only See text Examples 10.21, 10.22, 10.23, &

Multiple Inheritance We know that Java does not allow direct multiple inheritance. By using interfaces we can have multiple inheritance. Class TortoiseRacer inherits both the class Animal via the extends keyword and the interface that it implements. 18

Multiple Inheritance Class TortoiseNonRacer has single inheritance only (it does not implement the interface) 19

Multiple Inheritance Suppose we have abstract class Student (template class). – This class has abstract method getGPA which returns the gpa. Suppose we also have abstract class Employee (template class) – This class has abstract method getSalary which returns the salary. Class StudentEmployee wants to inherit both classes and implement both the gpa and the salary. It can not do that because multiple inheritance is not allowed public class StudentEmployee extends Student extends Employee ABOVE IS NOT LEGAL!!!!!!! 20

Multiple Inheritance Let us leave Employee intact and instead remove the abstract method getGPA from the Student class. – Thus there is no need for Student to be abstract any more. Let us create an interface : public interface StudentInterface defines the method getGPA Let us create a class StudentImpl which inherits Student and implements interface StudentInterface. It implements the method getGPA which calculates the gpa. 21

Multiple Inheritance Now class StudentEmployee extends Employee and it also implements the interface StudentInterface. Therefore it implements the method getGPA but kind of indirectly (via the implementation of the method in the StudentImpl class): StudentImpl st=new StudentImpl(); public float getGPA() { return st.getGPA(); } StudentEmployee object can invoke other methods of class Student since StudentImpl also inherits student. It also can use the class Employee since it inherited that class directly. 22

Multiple Inheritance 23 Abstract Employee class Student class Class StudentEmployee Uses StudentImpl StudentImpl Interface StudentInterface implements extends

Study Guide Chapter 10 – Sections 10.6, 10.7,