More on Inheritance. Interfaces & Abstract Classes

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

Java Object Oriented Design. CSC207 Fall 2014

What is Inheritance?

Interfaces CSC 123 Fall 2018 Howard Rosenthal

Inheritance, Polymorphism, and Interfaces

Chapter 14 Abstract Classes and Interfaces

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

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

Practice for Chapter 11

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

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

Inheritance -- Introduction

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

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Chapter 5 Object-Oriented Programming

Inheritance and Polymorphism

Object-Oriented Concepts and Design Principles

C08: Inheritance and Polymorphism

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

Arrays Classes & Methods, Inheritance

CS-202 Introduction to Object Oriented Programming

JAVA MOCK TEST JAVA MOCK TEST II

C++ Important Questions with Answers

Inheritance (continued) Inheritance

Chapter 5. Inheritance

Java How to Program, 8/e

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

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

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

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

CS111: PROGRAMMING LANGUAGE II

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

CMSC 132: Object-Oriented Programming II

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Chapter 6 Introduction to Defining Classes

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

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Chapter 10 Classes Continued. Fundamentals of Java

Inheritance and Interfaces

Lecture 4: Extending Classes. Concept

More Relationships Between Classes

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

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

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

25. Generic Programming

Java: introduction to object-oriented features

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

CS 251 Intermediate Programming Inheritance

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

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

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

ECE 122. Engineering Problem Solving with Java

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Programming Exercise 14: Inheritance and Polymorphism

OVERRIDING. 7/11/2015 Budditha Hettige 82

C08: Inheritance and Polymorphism

Motivations. Chapter 13 Abstract Classes and Interfaces

Inheritance Abstraction Polymorphism

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

CS304 Object Oriented Programming Final Term

[ L5P1] Object-Oriented Programming: Advanced Concepts

PROGRAMMING LANGUAGE 2

What are the characteristics of Object Oriented programming language?

INHERITANCE AND EXTENDING CLASSES

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

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

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

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

CSCE3193: Programming Paradigms

Chapter 10: Inheritance

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Object Oriented Programming: Based on slides from Skrien Chapter 2

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Inheritance and Polymorphism

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

ITI Introduction to Computing II

Inheritance (Outsource: )

First IS-A Relationship: Inheritance

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Polymorphism and Inheritance

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

CMSC 132: Object-Oriented Programming II

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

ITI Introduction to Computing II

Overriding Variables: Shadowing

Computer Science II (20073) Week 1: Review and Inheritance

CLASSES AND OBJECTS IN JAVA

CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming II (CS300)

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Lecture 18 CSE11 Fall 2013 Inheritance

ENCAPSULATION AND POLYMORPHISM

Subtype Polymorphism

Transcription:

More on Inheritance Interfaces & Abstract Classes

Java interfaces A Java interface is used to specify minimal functionality that a client requires of a server. A Java interface contains: method specifications named constant definitions. A Java interface does not contain: constructors, method bodies, instance variables.

interface definition interface InterfaceName { constant definitions } method declarations (without implementations). A method declaration is simply an access modifier, a return type, and a method signature followed by a semicolon. No objects can be constructed from it. Objects can be constructed from a class that implements an interface.

Example public interface MyInterface { public static final int aconstant = 32; public static final double pi = 3.14159; } public void methoda( int x ); public double methodb();

interface definition A class implements an interface by doing this: public class SomeClass implements interfacename { // fields, methods and constructors } // implementation of interface methods A class may implement several interfaces.

Notes A class that implements an interface must implement each method in the interface. Methods from the interface must be declared public in the class. Constants from the interface can be used as if they had been defined in the class. Constants should not be redefined in the class.

Modeling alternative implementations A game implementation models class Player responsible for making a move according to Game rules. Strategies Player can implement when making a move: Timid strategy Greedy strategy Clever strategy

Modeling alternative implementations Player abstraction and Player clients should be independent of implementations; independent of strategies chosen; implementations must respect the abstraction s contract: Player makes a move according to Game rules.

Java Interface <<Interface>> Player TimidPlayer GreedyPlayer CleverPlayer

Interface and types An interface defines a type. if it references an instance of a class that implements the interface. A value of type TimidPlayer, is also of type Player. A value of type GreedyPlayer, is also of type Player. A value of type CleverPlayer is also of type Player.

Interface and types The type TimidPlayer is said to be a subtype of the type Player. The type GreedyPlayer is said to be a subtype of the type Player. The type CleverPlayer is said to be a subtype of the type Player Player is a supertype of TimidPlayer. GreadyPlayer. CleverPlayer.

Interface and types A type defined by an interface can be used like any other reference type. It can be the type of an instance variable or parameter, It can be the return type of a query

Types and Subtypes If client expects server of type Player, then a value of any Player subtype can be provided. Subtype rules: if type A is a subtype of type B, then an A value can be provided wherever a B value is required. an A expression can be written wherever a B value is required

Example Consider a Game class with constructor: public Game (Player player1, Player player2) It can be invoked with arguments referencing TimidPlayers, GreedyPlayers, CleverPlayers. i.e player1 and/or player2 may be any of the subtypes

Static types The Game method nextplayer is specified as public Player nextplayer () If game is a Game instance, Player is the static type of expression game.nextplayer()

Dynamic types When game.nextplayer() is evaluated during execution, value returned will reference an specific object: If an instance of TimidPlayer. Then dynamic type of value returned by expression is TimidPlayer. If an instance of GreedyPlayer. Then dynamic type of value returned by expression is GreedyPlayer. If an instance of CleverPlayer. Then dynamic type of value returned by the expression is CleverPlayer. The dynamic type is always a subtype of Player.

Types and Subtypes Given private Player nextplayer; private void reportplay (Player player) public Player winner () The following require expressions of type Player nextplayer = Player expression required; reportplay( Player expression required); public Player winner () { return Player expression required;

Types and Subtypes Given TimidPlayer timid = newtimidplayer( Fred"); The following are legal nextplayer = timid; reportplay(timid); public Player winner () { return timid; }

Types and Subtypes If game is a Game instance, we cannot write the following TimidPlayer next = game.nextplayer(); Assignment operator requires a TimidPlayer on the right. game.nextplayer() is of type Player, and Player is not a subtype of TimidPlayer.

Review of Player interface <<Interface>> Player TimidPlayer GreedyPlayer CleverPlayer Interface Player implemented by several classes classes are identical except for implementation of taketurn. classes contain considerable amount of duplicate code.

Abstract classes AbstractPlayer <<Interface>> Player can contain implementations of methods common to all Player variants, Player subclasses inherit these methods. AbstractPlayer TimidPlayer GreedyPlayer CleverPlayer

Abstract classes An abstract class is a class: Defines a type. Occupies a position in the class hierarchy. Can be the parent of other classes, abstract or not. Has a unique parent which may or may not be abstract. Has one or more constructors. It can define nonabstract methods. It can contain abstract methods, Has instance variables. It cannot be instantiated. Concrete class: a non-abstract class.

Abstract class definition public abstract class ClassName {..... // definitions of methods and variables }

Abstract Methods An abstract method has no body. (It has no statements.) It declares an access modifier return type method signature followed by a semicolon. A non-abstract child class inherits the abstract method and must define a nonabstract method that matches the abstract method

Example public abstract class Item { } // fields as before // then methods public abstract void foomethod(int x) ; Each non-abstract class that extends Item must provide an implementation for foo

Example public abstract class AbstractPlayer implements Player { } private String name; // other attributes public AbstractPlayer (String name) { } public String name () { } // other fully defined methods public String tostring () { } public abstract void taketurn (Move move) ;

TimidPlayer definition class TimidPlayer extends AbstractPlayer { public TimidPlayer (String name) { super(name); } public void taketurn (Move move) { // details of the method } }

Interfaces, abstract classes and concrete classes An interface used to specify functionality required by a client. An abstract class provides a basis on which to build concrete servers. A concrete class completes server implementation specified by an interface; furnish run-time objects; not generally suited to serve as a basis for extension.

Abstract class use An abstract class factors out implementation of its concrete subclasses. Used to exploit polymorphism. Functionality specified in parent class can be given implementations appropriate to each concrete subclass. Abstract class must be stable. any change in an abstract class propagates to subclasses and their clients. A concrete class can only extend one (abstract) class

Interface use Interfaces are by definition abstract. separate an object s implementation from its specification. they do not fix any aspect of an implementation. A class can implement more than one interface. Interfaces allow a more generalized use of polymorphism; instances of relatively unrelated classes can be treated as identical for some specific purpose.

Composition Uses of composition a component is an intrinsic part of an object. a class formed as an aggregation of components, where components exist independently of aggregation. to wrap an existing class in order to alter its interface.

Class extension OR class composition? «interface» Player AbstractPlayer TimidPlayer GreedyPlayer CleverPlayer Player has-a «interface» PlayStrategy TimidStrategy GreedyStrategy CleverStrategy

public class Player { private String name; private PlayerStrategy strategy; // other attributes public AbstractPlayer (String name) { } public String name () { } // other fully defined methods public String tostring () { } } public void taketurn () { strategy.makemove(); }

Class extension v.s. class composition Two advantages of class extension code reuse polymorphism. Disadvantages of class extension Changes to a superclass specification propagate to clients and subclasses classes are not always designed and documented for extension. a subclass is committed to maintain specifications inherited from its superclass.

Class extension v.s. class composition Advantages of composition Existing classes can be used in composed classes. Can change object s behavior dynamically. Supports stronger encapsulation than does inheritance. Can change specification of composed class without changing core. Composed class depends only on specification of core class, not on its implementation. Implementation changes in core class do not propagate to composed class.