Inheritance. Inheritance

Similar documents
Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

Java Object Oriented Design. CSC207 Fall 2014

INHERITANCE. Spring 2019

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

CS-202 Introduction to Object Oriented Programming

Check out Polymorphism from SVN. Object & Polymorphism

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

Intro to Computer Science 2. Inheritance

Introduction to Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

JAVA MOCK TEST JAVA MOCK TEST II

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

Binghamton University. CS-140 Fall Chapter 9. Inheritance

ITI Introduction to Computing II

Inheritance. Transitivity

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

ITI Introduction to Computing II

Programming 2. Inheritance & Polymorphism

Programming II (CS300)

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

Inheritance, Polymorphism, and Interfaces

Inheritance (P1 2006/2007)

Programming II (CS300)

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

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Polymorphism. return a.doublevalue() + b.doublevalue();

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

8. Polymorphism and Inheritance

CMSC 132: Object-Oriented Programming II

Chapter 11 Classes Continued

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

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Methods Common to all Classes

2. (True/False) All methods in an interface must be declared public.

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

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

Chapter 14 Abstract Classes and Interfaces

Polymorphism and Inheritance

More On inheritance. What you can do in subclass regarding methods:

Polymorphism and Interfaces. CGS 3416 Spring 2018

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Java Magistère BFA

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

CMSC 132: Object-Oriented Programming II

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

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

CST242 Object-Oriented Programming Page 1

index.pdf January 21,

Chapter 13 Abstract Classes and Interfaces

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Chapter 10 Classes Continued. Fundamentals of Java

Programming Languages and Techniques (CIS120)

ITI Introduction to Computing II

OBJECT ORİENTATİON ENCAPSULATİON

CS 251 Intermediate Programming Inheritance

ITI Introduction to Computing II

More on Objects in JAVA TM

Abstract Classes and Interfaces

Object-Oriented Concepts

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

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

Lecture 18 CSE11 Fall 2013 Inheritance

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

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Inheritance and Polymorphism

Chapter 9 Inheritance

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

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

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

Making New instances of Classes

Java Fundamentals (II)

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

Inheritance (continued) Inheritance

More about inheritance

Handout 9 OO Inheritance.

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

The class Object. Lecture CS1122 Summer 2008

Inheritance and Interfaces

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

ENCAPSULATION AND POLYMORPHISM

CISC370: Inheritance

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

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

Chapter 6 Introduction to Defining Classes

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Object typing and subtypes

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

Inheritance and object compatibility

public class SomeClass OtherClass SomeInterface { }

Java Classes, Inheritance, and Interfaces

First IS-A Relationship: Inheritance

Inheritance. The Java Platform Class Hierarchy

Overriding Variables: Shadowing

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Transcription:

1 2 1

is a mechanism for enhancing existing classes. It allows to extend the description of an existing class by adding new attributes and new methods. For example: class ColoredRectangle extends Rectangle { //here comes the class body of ColoredRectangle Rectangle is called superclass and ColoredRectangle subclass. One important reason: code reuse. 3 Example: import java.awt.color; class Rectangle { private int x, y, height, width; public void draw() { public void move(int dx, int dy){ class ColoredRectangle extends Rectangle { private Color FillColor; public Color getfillcolor() { return this.fillcolor; public void setfillcolor(color c) { this.fillcolor = c; public void move(int dx, int dy){ 4 2

The subclass inherits all the methods of its superclass. For example, the following methods calls are possible: ColoredRectangle r = new ColoredRectangle(); r.left(); r.move(10.0, 20.0); Additionally, in the subclass new methods can defined and used by subclass-objects. r.setfillcolor(color.gray); Additionally, in the subclass methods of the superclass canbeoverwritten. Attributes of the subclass, that are instance variables, can be newly defined or they can be inherited of the superclass (but not overwritten). 5 import java.awt.color; class ColoredRectangle extends Rectangle { private Color FillColor; public Color getfillcolor() { return this.fillcolor; public void setfillcolor(color c) { this.fillcolor = c; public void draw() { //draws the rectangle and fills it with color 6 3

import java.awt.color; class ColoredRectangle extends Rectangle { private Color FillColor; public ColoredRectangle(Color c){ //initializes a new colored rectangle super(); //that must be first statement of the subclass constructor FillColor = c; public Color getfillcolor() { return this.fillcolor; public void setfillcolor(color c) { this.fillcolor = c; public void draw() { //draws the rectangle and fills it with color super.draw(); 7 Examples: (in chapter 13) SavingsAccount (Sparkonto) CheckingAccount (Girokonto) 8 4

9 Generalization Spezilization 10 5

Common Error Shadowing Instance Fields, for example 11 - IsRelation Polymorphism: Rectangle Rectangle r; ColoredRectangle c; r = new ColoredRectangle(Color.blue); //o.k. c = r; c = (ColoredRectangle)r; Coloured Rectangle //error //o.k. 12 6

Handling and Late Binding: Rectangle rect[]; rect = new Rectangle[100]; //Objekte werden den Referenzen zugewiesen rect[2]= new ColoredRectangle(); for (i=0; i<rect.length; i++) { rect[i].move(dx,dy); rect[i].draw(); 13 Abstract methods and abstract classes: An abstract class is class from which no objects can be created. An abstract method is a method without any body, that is with no implementation. abstract public void draw(); A class which has an abstract method has to be declared abstract. A class without abstract methods can also be declared abstract. Why abstract classes? 14 7

Example: GraphicObject is a superclass for objects which can be drawn and moved on a panel. The draw method can not be implemented, since it is dependent from the concrete subclass objects: It has to be declared abstract. abstract public class GraphikObject{ double x, y; public void move(double dx, double dy){ x+=dx; y+=dy; abstract public void draw(); class Rectangle extends GraphikObject { public void draw() { //owverwrite and implements the abstract method //of the superclass GraphikObject 15 Interfaces revisited: Remember our class ColoredRectangle: class ColoredRectangle extends Rectangle{ What to do if this class should also be a subclass of GeometricObject? Perhaps: class ColoredRectangle() extends Rectangle, GeometricObject { //error Multiple inheritance is not possible in Java! Use an interface: interface GeometricObject { class ColoredRectangle() extends Rectangle implements GeometricObject { //o.k. 16 8

Key word: final This key word can also be used in class definition public final class String { That means that class String can not be extended (by a subclass). There are two reasons: 1. The compiler can generate more efficient method calls (no late binding) 2. Also the String class is meant to be immutable: string objects can not be modified by their own methods. All String references can be copied without the risk of mutation. 17 Key word: final This key word can also be used in method definition public class SecureAccount extends BankAccount { public final boolean checkpassword(string ){ That means that it is not possible to override this method, that is a method with the same signature, by a method in a subclass. 18 9

Object: The cosmic superclass. Object is superclass of every other class. It contains the methods: String tostring(): Returns a string representation of the object. Used in Rectangle cerealbox = new Rectangle(5, 10, 20, 30); cerealbox= + cerealbox; boolean equals(object otherobject): Tests whether the object equals another object. Object clone(): Makes a full copy of an object. 19 20 10

equals-method The test (coin1 == coin2) is not sufficient, since it is checked whether two references are equal 21 But: We want to know whether: Object 1 and object 2 have the same state. Use the method: boolean equals(object) Make a previous check with getclass()! 22 11

If the clone()-method of the class Object is used. 23 A deep copy is gained by recursively calling the clone()-method for all referenced objetcs. As a result, the account-object in our example is also cloned. Note, the Cloneable-interface has to be implemented, if clone() is implemented, and the clone()-method of Object can throw exceptions. 24 12