COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Similar documents
Object-Oriented Programming More Inheritance

Chapter 14 Abstract Classes and Interfaces

Object-Oriented ProgrammingInheritance & Polymorphism

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Interface Class. Lecture 22. Based on Slides of Dr. Norazah Yusof

Java Session. Day 2. Reference: Head First Java

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Polymorphism. Arizona State University 1

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Inheritance & Polymorphism. Object-Oriented Programming

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Inheritance & Polymorphism

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Inheritance (Outsource: )

Software Development (cs2500)

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 4 09/03/2009

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

What is Inheritance?

C09: Interface and Abstract Class and Method

ECE 122. Engineering Problem Solving with Java

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Inheritance and Polymorphism

C18a: Abstract Class and Method

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

OVERRIDING. 7/11/2015 Budditha Hettige 82

OOP in Java Review. CS356 Object-Oriented Design and Programming October 1, 2014

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Programming II (CS300)

OLLSCOIL NA heireann THE NATIONAL UNIVERSITY OF IRELAND, CORK. COLAISTE NA hollscoile, CORCAIGH UNIVERSITY COLLEGE, CORK

More on Inheritance. Interfaces & Abstract Classes

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CS111: PROGRAMMING LANGUAGE II

Programming overview

Chapter 5. Inheritance

Programming II (CS300)

CSA 1019 Imperative and OO Programming

COMP 110/L Lecture 20. Kyle Dewey

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Inheritance, Polymorphism, and Interfaces

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

CS313D: ADVANCED PROGRAMMING LANGUAGE

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Programming Using C# QUEEN S UNIVERSITY BELFAST. Practical Week 7

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

VIRTUAL FUNCTIONS Chapter 10

Inheritance, polymorphism, interfaces

JAVA MOCK TEST JAVA MOCK TEST II

Object Oriented Programming. Java-Lecture 11 Polymorphism

First IS-A Relationship: Inheritance

Class, Variable, Constructor, Object, Method Questions

CPSC 324 Topics in Java Programming

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.

Introducing Java. Introducing Java. Abstract Classes, Interfaces and Enhancement of Polymorphism. Abstract Classes

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

COMP 250 Fall inheritance Nov. 17, 2017

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

Object-Oriented Programming

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

CS313D: ADVANCED PROGRAMMING LANGUAGE

More Relationships Between Classes

C10: Garbage Collection and Constructors

Java Object Oriented Design. CSC207 Fall 2014

Motivations. Chapter 13 Abstract Classes and Interfaces

CS 116 Week 8 Page 1

Arrays Classes & Methods, Inheritance

Chapter 10 Classes Continued. Fundamentals of Java

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Chapter 13 Abstract Classes and Interfaces

Lecture 5: Inheritance

COE318 Lecture Notes Week 8 (Oct 24, 2011)

type conversion polymorphism (intro only) Class class

Introduction to Object-Oriented Programming

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Object Orientated Programming Details COMP360

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

Inheritance. Transitivity

Logistics. Final Exam on Friday at 3pm in CHEM 102

STATIC, ABSTRACT, AND INTERFACE

Transcription:

1 1 COMP200 INTERFACES OOP using Java, from slides by Shayan Javed

Interfaces 2

ANIMAL picture food sleep() roam() makenoise() eat() 3

ANIMAL picture food sleep() roam() makenoise() eat() 4 roam() FELINE roam() CANINE

ANIMAL picture food sleep() roam() makenoise() eat() 5 roam() FELINE roam() CANINE LION makenoise() eat() CAT makenoise() eat() WOLF makenoise() eat() DOG makenoise() eat()

6 Problem Need to add Pet behaviors to the pet animal classes (Cat and Dog): play()

7 Option 1 Animal Add concrete methods: play() to Animal class Feline Canine Lion Cat Wolf Dog

8 Option 1 Animal Add concrete methods: play() to Animal class Feline Canine What s the problem? Lion Cat Wolf Dog

9 Option 1 Animal Add concrete methods: play() to Animal class Feline Canine What s the problem? Lion Cat Wolf Dog Also exist for Lion/Wolf

10 Option 2 Animal Add abstract methods: play() to Animal class Feline Canine Lion Cat Wolf Dog

11 Option 2 Animal Add abstract methods: play() to Animal class Feline Canine Provide empty body for it in Lion and Wolf classes Lion Cat Wolf Dog

12 Option 2 Animal Add abstract methods: play() to Animal class Feline Canine Provide empty body for it in Lion and Wolf classes Lion Cat Wolf Dog Provide non-empty body for it in Cat and Dog classes

13 Option 3 Feline Animal Canine Add concrete methods: play() to Cat and Dog classes Lion Cat Wolf Dog

14 What we need Pet behaviors just in Pet classes (like Dog and Cat)

15 What we need Pet behaviors just in Pet classes (like Dog and Cat) Consistency - Guarantee that all Pet classes use the same signature for Pet methods

16 What we need Pet behaviors just in Pet classes (like Dog and Cat) Consistency - Guarantee that all Pet classes use the same signature for Pet methods Take advantage of Polymorphism

17 Multiple Inheritance? Animal Pet Feline Canine Lion Cat Wolf Dog

18 Multiple Inheritance? Animal Pet Feline Canine Lion Cat Wolf Dog But Java does not allow multiple inheritance! Why?

19 Multiple Inheritance? Not allowed Mainly to avoid complexity/confusion. Want to keep it simple.

20 The Diamond Problem SuperClass method() A method() method() B C

21 The Diamond Problem SuperClass method() A method() method() B C What happens when method() is called in C?

22 Solution: Interfaces Make Pet an interface and put all pet behaviors in it as abstract methods Animal Pet abstract play(); Feline Canine Lion Cat Wolf Dog

23 Interfaces Classlike construct contains only constants and abstract methods.

24 Interfaces Classlike construct contains only constants and abstract methods. cannot contain variables or concrete methods.

25 Interfaces Classlike construct contains only constants and abstract methods. cannot contain variables or concrete methods. Declaration: public interface <InterfaceName> { // constant declarations; } // method signatures;

26 Interface Pet public interface Pet { } public void play();

27 Implementing Pet interface class Dog extends Canine implements Pet{ } public void play(){ System.out.println( Catch ball ); }

28 Implementing Pet interface class Dog extends Canine implements Pet{ } public void play(){ System.out.println( Catch ball ); } class Cat extends Feline implements Pet { } public void play(){ System.out.println( Roll ball ); }

29 Interface Characteristics Interfaces are defined in their own file

30 Interface Characteristics Interfaces are defined in their own file Methods are abstract. No implementations.! May omit the abstract keyword.

31 Interface Characteristics Interfaces are defined in their own file Methods are abstract. No implementations.! May omit the abstract keyword. Do not have instance fields.

32 Interface Characteristics Interfaces are defined in their own file Methods are abstract. No implementations.! May omit the abstract keyword. Do not have instance fields. Can have constant fields. Automatically treated as public, static, final; may omit any of these keywords

33 Example public interface Interface1 { int anumber = 0; } public void amethod();

34 Example public interface Interface1 { int anumber = 0; } public void amethod(); Same as: public interface Interface1 { public static final int anumber = 0; } public abstract void amethod();

35 Interface Characteristics A class can implement any number of interfaces public class aclass implements I1, I2, I3 { // methods from I1, I2, I3... }

36 Interface Characteristics A class can implement any number of interfaces public class aclass implements I1, I2, I3 { // methods from I1, I2, I3... } Interfaces can not be instantiated

37 Interface Characteristics A class can implement any number of interfaces public class aclass implements I1, I2, I3 { // methods from I1, I2, I3... } Interfaces can not be instantiated But can be used as a data type for a variable

38 Using the interface Pet[] pets = new Pet[2]; pet[0] = new Dog(); pet[1] = new Cat(); for(int i = 0; i < pets.length; i ++) pet[i].play();

39 Using the interface Pet[] pets = new Pet[2]; pet[0] = new Dog(); pet[1] = new Cat(); for(int i = 0; i < pets.length; i++) pet[i].play(); Similar to abstract classes advantage of polymorphism

40 Classes from different inheritance hierarchy can implement same interface! Robot Feline Animal Canine Pet abstract play(); RoboDog Roomba Lion Cat Wolf Dog

41 Abstract Classes vs. Interfaces Variables Constructors Methods Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. No restrictions. An abstract class cannot be instantiated using the new operator. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods

42 Abstract Classes vs. Interfaces Variables Constructors Methods Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. No restrictions. An abstract class cannot be instantiated using the new operator. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods

43 Abstract Classes vs. Interfaces Variables Constructors Methods Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. No restrictions. An abstract class cannot be instantiated using the new operator. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods

44 Abstract Classes vs. Interfaces Cannot inherit from multiple classes but, Can can implement multiple interfaces Interfaces = model can-do type relationships Inheritance = model is-a type of relationship

45 Next Lecture More on interfaces