CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
CPSC150 Abstract Classes and Interfaces Chapter 10.
1 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES LECTURE 1 GEORGE KOUTSOGIANNAKIS George Koutsogiannakis / Summer 2011.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
©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 and Interfaces. Abstract classes.
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,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.,
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
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.
Further Abstraction Techniques Chapter 10. Abstract Classes There are times when you do not want someone to be able to make an object of your class. For.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
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 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
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Sections Inheritance and Abstract Classes
Abstract Classes and Interfaces
Abstract Classes and Interfaces
University of Central Florida COP 3330 Object Oriented Programming
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES
UML Class Diagram: class Rectangle
Chapter 13 Abstract Classes and Interfaces
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9: Polymorphism and Inheritance
Interfaces.
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Advanced Java Programming
Inheritance Inheritance is a fundamental Object Oriented concept
Inheritance.
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Final and Abstract Classes
Abstract Classes and Interfaces
Chapter 11 Inheritance and Encapsulation and Polymorphism
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 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer

Topics Interfaces (Textbook: Chapter 10.8)

Review of Inheritance Advantages of Inheritance Code reuse Superclass encapsulates the common methods and variables Subclass inherits the public/protected methods Specialization: Subclass can add new methods or override superclass methods Force a design Abstract class force its subclass to implement abstract methods

An example of Inheritance: A Graphics program (from textbook) Racer +ID: int +X: int +Y: int +Constructors +move() +draw() Tortoise speed: int +Constructors +move() +draw() *Abstract methods and classes are underlined in the figure See text Examples 10.21, 10.22, 10.23, & 10.24

An example of Inheritance: A Graphics program (from textbook) //abstract superclass public abstract class Racer { public abstract void move(); public abstract void draw( Graphics g ); } //subclass public class Tortoise extends Racer { public void move(){ //code to calculates the new position for the racer } public abstract void draw( Graphics g ){ //code to draw the racer at the new position }

An example of Inheritance: A Graphics program (from textbook) //abstract superclass public abstract class Racer { int x; int y; //other variables public abstract void move(); public abstract void draw( Graphics g ); //other methods } //subclass public class Tortoise extends Racer { public void move(){ //code to calculates the new position for the racer } public abstract void draw( Graphics g ){ //code to draw the racer at the new position } //other methods }

An example of Inheritance: A Graphics program (from textbook) //subclass public class Tortoise extends Racer { public void move(){ //code to calculates the new position for the racer } public void draw( Graphics g ){ //code to draw the racer at the new position } public void draw( Graphics g ) { int startX = getX( ); int startY = getY( ); g.setColor( new Color( 34, 139, 34 ) ); // dark green //body g.fillOval( startX - 30, startY, 25, 15 ); //head g.fillOval( startX - 10, startY + 5, 15, 10 ); //flatten bottom g.clearRect( startX - 30, startY + 11, 35, 4 ); //more code }

An example of Inheritance: A Graphics program (from textbook) We now ALSO have tortoise figures which are static. They don’t need the move() method Static and Dynamic tortoise figures have common methods (draw()) Also an additional method (move()) for the dynamic tortoise figure See text Examples 10.21, 10.22, 10.23, & Racer +ID: int +X: int +Y: int +Constructors +move() +draw() Tortoise speed: int +Constructors +move() +draw()

How about this design? Animal +ID: int +X: int +Y: int +Constructors +draw() TortoiseNonRacer speed: int +Constructors +draw() Animal +ID: int +X: int +Y: int +Constructors +draw() TortoiseRacer speed: int +Constructors +move() +draw() Moveable +Constructors +move() inheritance from multiple superclasses not allowed!!! Modify Racer and have only abstract method draw()

Interfaces To allow a class to inherit behavior from multiple sources, Java provides the interface. The intent of an interface is to specify a set of methods that a concrete class will honor. A class can inherit directly from only one class, that is, a class can extend only one class. An interface typically specifies behavior that a class will implement. Interface members can be any of the following:  Abstract methods  Classes  Constants  other interfaces 10

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

An example of Inheritance: A Graphics program (from textbook) Animal +ID: int +X: int +Y: int +Constructors +draw() TortoiseRacer speed: int +Constructors +move() +draw() Moveable (interface) +move() Create an interface that has the abstract method move()

A Graphics program (from textbook) public interface Moveable { //variables void move( ); // abstract method } public class TortoiseRacer extends Animal implements Moveable { //variables //constructors public void move(){ //code to calculates the new position for the racer } public void draw( Graphics g ){ //code to draw the racer at the new position } Animal +ID: int +X: int +Y: int +Constructors +draw() TortoiseRacer speed: int +Constructors +move() +draw() Moveable (interface) +move()

Finer Points of Interfaces An interface's fields (variables) are public, static, and final. These keywords can be specified or omitted. When you define a field (variable) 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. 14

Inheriting from Multiple Interfaces To inherit from multiple interface, a class declares that it implements the interfaces 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). 15

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

Differences between Abstract class and Interface Abstract classInterface 1) Abstract class can have abstract and non- abstract methods. Interface can have only abstract methods. 2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance. 3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables. 4) Abstract class can have constructor.Interface can't have constructor. 5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.

Multiple Inheritance (Student Employee Example) 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!!!!!!! 18

Student +ID: int +Constructors +getGPA) StudentEmployee +Constructors +getGPA() +getSalary() Employee +Constructors +getSalary() Suppose we have abstract class Student. This class has abstract method getGPA() which returns the gpa. Suppose we also have abstract class Employee 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. Inheritance from multiple superclasses not allowed!!! Multiple Inheritance (Student Employee Example)

StudentInterface (interface) +getGPA) StudentEmployee +Constructors +getGPA() +getSalary() Employee +Constructors +getSalary() How about creating an interface that has the abstract getGPA() method and have the Employee class implement it? Won’t work? Why?

Multiple Inheritance (Student Employee Example) StudentInterface (interface) +getGPA) StudentEmployee +Constructors +getGPA() +getSalary() Employee +Constructors +getSalary() getGPA() method in class studentEmployee needs details from the class student Solution: have a class attribute of type student or a subclass of student

Multiple Inheritance Let us 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. 22 Student +ID: int +Constructors StudentInterface (interface) +getGPA() StudentImpl +Constructors +getGPA()

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

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