Reusing Classes. Hendrik Speleers

Similar documents
Polymorphism. OOAD 1998/99 Claudia Niederée, Joachim W. Schmidt Software Systems Institute

ITI Introduction to Computing II

ITI Introduction to Computing II

8. Interfaces & Inner Classes

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Reusing Classes in Java. Java - Inheritance/Polymorphism/Interface. Inheritance. Simple Example of Composition. Composition.

Person class. A class can be derived from an existing class by using the form

Person class. A class can be derived from an existing class by using the form

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Java Object Oriented Design. CSC207 Fall 2014

Inheritance (Deitel chapter 9)

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

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

Making New instances of Classes

Programming 2. Inheritance & Polymorphism

Lecture Notes Chapter #9_b Inheritance & Polymorphism

CS1150 Principles of Computer Science Objects and Classes

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

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

Programming Languages and Techniques (CIS120)

Inheritance. OOP: Inheritance 1

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

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

Programming Languages and Techniques (CIS120)

Advanced Placement Computer Science. Inheritance and Polymorphism

Inheritance and Interfaces

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

Chapter 14 Abstract Classes and Interfaces

Inheritance and Polymorphism

Chapter 9 - Object-Oriented Programming: Polymorphism

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

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

Programming Languages and Techniques (CIS120)

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

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

First IS-A Relationship: Inheritance

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Inheritance Motivation

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

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

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Overriding Variables: Shadowing

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

More Relationships Between Classes

Inheritance, Polymorphism, and Interfaces

Inheritance and Polymorphism

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

COMP 250. inheritance (cont.) interfaces abstract classes

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

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

Chapter 3: Inheritance and Polymorphism

Chapter 6: Inheritance

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

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

CS-202 Introduction to Object Oriented Programming

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Inheritance -- Introduction

Exercise: Singleton 1

Inheritance and Encapsulation. Amit Gupta

Programming Languages and Techniques (CIS120)

Reviewing OO Concepts

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

Example: Count of Points

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Arrays Classes & Methods, Inheritance

Object Oriented Software Design

Object Oriented Software Design

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

CMSC 132: Object-Oriented Programming II

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

Chapter 13 Abstract Classes and Interfaces

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

Inheritance, and Polymorphism.

CS 251 Intermediate Programming Inheritance

Chapter 9 - Object-Oriented Programming: Inheritance

Methods Common to all Classes

Programming Exercise 14: Inheritance and Polymorphism

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

Inheritance and Polymorphism

Example: Count of Points

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Principles of Software Construction: Objects, Design and Concurrency. Packages and Polymorphism. toad Fall 2012

CMSC 132: Object-Oriented Programming II

Polymorphism and Inheritance

8. Polymorphism and Inheritance

Object Oriented Paradigm

Transcription:

Hendrik Speleers

Overview Composition Inheritance Polymorphism Method overloading vs. overriding Visibility of variables and methods Specification of a contract Abstract classes, interfaces

Software development One of the holy grails of OOP: reusing classes When you need a class, you can Get the perfect one off the shelf» one extreme e.g., library, GUI builder environment Write it completely from scratch» other extreme Reuse an existing class with composition Reuse an existing class or class framework with inheritance A good class design is important

Composition Simplest way to reuse existing code Instances of existing classes inside a new class Flexibility: can change objects at runtime A has-a relationship between classes class MyNewClass { Foo x = new Foo(); Bar x = new Bar(); Baz x = new Baz();... MyNewClass Instance of existing class 1 Instance of existing class 2 etc.

Inheritance Pure inheritance Interface duplication for interchangeable objects Redefinition of methods with the same interface An is-a relationship between classes Extension inheritance Inheritance to extend the interface ParentClass method1() method2() reuse new Additional variables and methods An is-like-a relationship between classes Single inheritance (e.g., Java) vs. Multiple inheritance (e.g., C++) ChildClass1 method1() method2() ChildClass2 method1() method2() method3()

Inheritance Terminology Parent, superclass, base class,... Child, subclass, derived class,... Class Object is the root of the class hierarchy Every class has Object as a superclass A class can have at most one parent but of course more ancestors Creating a subclass by the extends keyword class <class name> extends <class name>

Inheritance Example: the classes Shape, Circle, Rectangle Shape Circle Rectangle getcenter() movecenterto() movecenterby() tostring() getarea() getperimeter() getcenter() movecenterto() movecenterby() tostring() getarea() getperimeter() getcenter() movecenterto() movecenterby() tostring() getarea() getperimeter() a Circle is a Shape a Rectangle is a Shape getradius() setradius()... getwidth() getheight()...

Inheritance Example: the classes Shape, Circle, Rectangle public class Shape { protected Point center; public Shape() { center = new Point(); public Shape(int x, int y) { center = new Point(x, y); public Point getcenter() { return center; public void movecenterto(int x, int y) { center.setlocation(x, y); public double getarea() { return 0.0;...

Inheritance Example: the classes Shape, Circle, Rectangle public class Point { protected int coordx, coordy; public Point() { setlocation(0, 0); public Point(int x, int y) { setlocation(x, y); public void setlocation(int x, int y) { coordx = x; coordy = y; public int getx() { return coordx; public int gety() { return coordy;...

Inheritance Example: the classes Shape, Circle, Rectangle public class Circle extends Shape { protected double radius; inherits from Shape the methods getcenter, movecenterto,... but NOT the constructors public Circle() { center = new Point(); radius = 1.0; public Circle(int x, int y, double r) { center = new Point(x, y); radius = r; public double getradius() { return radius; public double getarea() { return Math.PI * radius * radius;... initialize class variables of class and superclass

Polymorphism Upcasting A variable of class X can refer to objects of class X or any of its subclasses Shape shape; shape = new Shape(); shape = new Circle(1, 0, 2.5); Separation of interface from implementation Substitutability Extensibility

Polymorphism Upcasting A variable of class X can refer to objects of class X or any of its subclasses Substitutability getcenter() getarea() getperimeter()... Shape Circle Rectangle

Polymorphism Upcasting A variable of class X can refer to objects of class X or any of its subclasses Extensibility getcenter() getarea() getperimeter()... Shape Circle Rectangle Triangle

Method overriding versus overloading Method binding = connecting a method call to a method body Method overriding Redefinition of a (parent) method with exactly the same interface Same name, same number of parameters, same type of parameters Dynamic binding (at run-time) Method overloading Redefinition of a method with a similar interface Same name, but different set of parameters (number and/or type) Static binding (at compile time)

Visibility of variables and methods Recall: public private protected Public: visible to the world (everybody outside and inside the class) Private: visible only to the class Protected: visible to the package and all subclasses Default (friendly), no keyword: visible to the package Use public or protected to be accessible to subclasses Good access strategy Limit direct access to variables Control access via Getters/Setters (methods)

Visibility of variables and methods Name conflicts of variables inside methods Priority: local variables > parameters > class variables > parent class variables Name conflicts of methods Priority: methods > parent methods Name conflicts can be avoided by a self-referencing pointer The this keyword The super keyword

Visibility of variables and methods The this keyword is a reference to the current object Referring to class variables Referring to methods inside class (no extra functionality) public class Circle extends Shape { protected double radius; public void setradius(double radius) { this.radius = radius;

Visibility of variables and methods Constructor delegation using the this keyword This guarantees consistent initialization It has to be the first statement in the constructor public class Circle extends Shape { public Circle() { this(0, 0, 1.0); public Circle(int x, int y, double r) { center = new Point(x, y); radius = r;

Visibility of variables and methods The super keyword is a reference to the current parent object Referring to parent class variables Referring to parent methods public class Shape { public String tostring() { return "shape (area " + getarea() + ")"; public class Circle extends Shape { public String tostring() { return "circle " + super.tostring();

Visibility of variables and methods Constructor delegation using the super keyword Constructors are not inherited; implicit default call super() if available It has to be the first statement in the constructor public class Shape { public Shape(int x, int y) { center = new Point(x, y); public class Circle extends Shape { public Circle(int x, int y, double r) { super(x, y); radius = r;

Specification of a contract Separation of interface from implementation Abstract classes using the abstract keyword an is-a or is-like-a relationship One or more methods in the class have no implementation Pure interfaces using the interface keyword a can-do relationship No method has an implementation A class can implement One abstract class via the extends keyword Multiple interfaces via the implements keyword

Specification of a contract Abstract classes Postponing implementation till where it makes sense Creating objects can only through (not abstract) subclasses public abstract class Shape {... public abstract double getarea(); public class Circle extends Shape {... public double getarea() { return Math.PI * radius * radius;

Specification of a contract Abstract classes Postponing implementation till where it makes sense Creating objects can only through (not abstract) subclasses Shape getcenter() movecenterto() movecenterby() tostring() getarea() getperimeter() UML: italic font type for abstract classes/methods

Specification of a contract Pure interfaces A class can implement multiple interfaces Interfaces can inherit from other interfaces public interface Figure { public void movecenterto(int x, int y); public class Shape implements Figure {... public void movecenterto(int x, int y) { center.setlocation(x, y);

Specification of a contract Pure interfaces A class can implement multiple interfaces Interfaces can inherit from other interfaces Circle Figure Shape Rectangle UML: difference in Interface Inheritance Triangle

Specification of a contract Example: Adventure CanFight CanSwim CanFly ActionMan Hero

Specification of a contract Example: Adventure public interface CanFight { public void fight(); public interface CanSwim { public void swim(); public interface CanFly { public void fly();

Specification of a contract Example: Adventure public class ActionMan implements CanFight { public void fight() { perform("fight!"); protected void perform(string action) { System.out.println(action); public class Hero extends ActionMan implements CanSwim, CanFly { public void swim() { perform("swim!"); public void fly() { perform("fly!");

Specification of a contract Example: Adventure public class Adventure { public static void t(canfight x) { x.fight(); public static void u(canswim x) { x.swim(); public static void v(canfly x) { x.fly(); public static void main(string[] args) { Hero h = new Hero(); t(h); // Treat it as a CanFight u(h); // Treat it as a CanSwim v(h); // Treat it as a CanFly

Specification of a contract Pure interfaces Class variables are automatically static and final An interface is convenient to create groups of constants (like enum) public interface Months { int JANUARY = 1, FEBRUARY = 2, MARCH = 3, APRIL = 4, MAY = 5, JUNE = 6, JULY = 7, AUGUST = 8, SEPTEMBER = 9, OCTOBER = 10, NOVEMBER = 11, DECEMBER = 12;

Example: interfaces Look at the file InterFaceEx.java Make a class diagram of all the involved classes/interfaces Predict the output of the main method