OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming
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.
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 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES LECTURE 1 GEORGE KOUTSOGIANNAKIS George Koutsogiannakis / Summer 2011.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
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.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
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.
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.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Sixth Lecture ArrayList Abstract Class and Interface
Final and Abstract Classes
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
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
Java Programming Language
Chapter 9: Polymorphism and Inheritance
Interfaces.
Polymorphism, Abstract Classes & Interfaces
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Inheritance.
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Chapter 8 Inheritance Part 2.
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Topics OOP Review Inheritance Review 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:

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

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

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

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.

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

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.

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 } ……………………}

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.

Example Example 10.19 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<Figure> figuresList = new ArrayList<Figure>( ); 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.

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

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

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.

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.

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).

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.

Example We define an abstract class Animal with one abstract method (See Example 10.22): 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 }

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

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 .

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

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!!!!!!!

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.

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.

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

Study Guide Chapter 10 Sections 10.6, 10.7, 10.8