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

Similar documents
Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

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

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

CS-202 Introduction to Object Oriented Programming

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

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

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

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

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

Inheritance and Polymorphism

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

ITI Introduction to Computing II

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

ITI Introduction to Computing II

Methods Common to all Classes

Making New instances of Classes

COMP 250. inheritance (cont.) interfaces abstract classes

CS1150 Principles of Computer Science Objects and Classes

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Chapter 14 Abstract Classes and Interfaces

Java Object Oriented Design. CSC207 Fall 2014

ITI Introduction to Computing II

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

ITI Introduction to Computing II

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Chapter 11 Inheritance and Polymorphism

index.pdf January 21,

CH. 2 OBJECT-ORIENTED PROGRAMMING

CISC370: Inheritance

Programming 2. Inheritance & Polymorphism

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

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Chapter 6: Inheritance

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

Chapter 11 Inheritance and Polymorphism

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

Java Magistère BFA

JAVA MOCK TEST JAVA MOCK TEST II

Advanced Placement Computer Science. Inheritance and Polymorphism

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Super-Classes and sub-classes

25. Generic Programming

Java Comparable interface

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

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Inheritance and Polymorphism

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

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

Rules and syntax for inheritance. The boring stuff

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

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

Polymorphism and Interfaces. CGS 3416 Spring 2018

We are on the GUI fast track path

CSE 143 Lecture 20. Circle

INHERITANCE. Spring 2019

Inheritance and Encapsulation. Amit Gupta

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

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

OBJECT ORİENTATİON ENCAPSULATİON

CS 251 Intermediate Programming Inheritance

Reusing Classes. Hendrik Speleers

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

CS171:Introduction to Computer Science II. Li Xiong

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

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

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

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

C12a: The Object Superclass and Selected Methods

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

Inheritance Sort in ascending order. Reusability 5 Sort Take The 4 Order 12,10,5,4. Class. Use this class to define a new class

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

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

Chapter 9 Inheritance

Introduction to Inheritance

Inheritance -- Introduction

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Abstract Classes Interfaces

Programming Languages and Techniques (CIS120)

CLASS DESIGN. Objectives MODULE 4

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

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

PROGRAMMING LANGUAGE 2

Example: Count of Points

Inheritance and object compatibility

Polymorphism and Inheritance

Abstract Classes and Interfaces

CMSC 132: Object-Oriented Programming II

Learn more about our research, discover data science, and find other great resources at:

Transcription:

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

2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every class = subclass of Object.

3 Inheritance Code reuse methods and properties. Use classes to model objects of the same type Define general class! Then define specialised classes.

4 Inheritance Can only inherit from one class. Can inherit from multiple classes in other languages (C++)

5 Example Let s say you create the following classes:! Circle! Rectangle! Triangle! etc. Share certain properties, but also specialised properties

6 Example public class GeometricObject { private String Color; private String name; private float area; // constructors... public GeometricObject(String color, String name) { // get/set methods, etc.

7 Example public class Circle extends GeometricObject { private double radius; // specific property public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; // get/set methods, etc. public double getarea() { return radius * radius * Math.PI;

8 Example An error in the constructor! public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; Why can t we use this.color and this.name?

9 Example They are private properties. NOT accessible by subclasses. Should use setcolor() instead. public methods/properties inherited.

10 Example public class Rectangle extends GeometricObject { private double width, height; // specific properties public Rectangle(double w, double h, String color, String name) { this.height = h; this.width = w; setcolor(color); setname(name); // get/set methods, etc. public double getarea() { return width * height;

11 The super keyword Are constructors inherited? Yes. super used for:! calling a super class constructor! calling a super class method

12 The super keyword Constructors: super() calls the default constructor super(arguments) calls the constructors according to the arguments

13 The super keyword Constructors: public Circle(double radius, String color, String name) { super(color, name); this.radius = radius;

14 The super keyword Constructors: public Circle(double radius, String color, String name) { super(color, name); this.radius = radius; public GeometricObject(String color, String name) { this.color = color; this.name = name;

15 The super keyword What if we don t add the super(..) constructor call?

16 The super keyword public Circle(double radius, String color, String name) { this.radius = radius;

17 The super keyword public Circle(double radius, String color, String name) { this.radius = radius; Is the same as: public Circle(double radius, String color, String name) { super(); this.radius = radius;

18 The super keyword Called Constructor Chaining

19 The super keyword Calling the super class methods super.method(parameters)... Optional

20 Protected fields Using get/set methods to access properties of the superclass is cumbersome

21 The protected keyword Often want subclasses to directly access properties/methods Use protected instead of private/public Subclasses can now directly access

22 Private public class GeometricObject { private String Color; private String name; private float area; // constructors... // get/set methods, etc.

23 The protected keyword public class GeometricObject { protected String Color; protected String name; protected float area; // constructors... // get/set methods, etc.

24 The protected keyword public class Circle extends GeometricObject { private double radius; // specific property public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; Now works!

25 Polymorphism Important aspect of OOP

26 Polymorphism Important aspect of OOP capability of an action or method to do different things based on the object that it is acting upon.

27 Polymorphism Important aspect of OOP capability of an action or method to do different things based on the object that it is acting upon. characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

28 Polymorphism 1. Overriding Methods 2. Overloaded methods 3. Dynamic (late) method binding

29 Overriding Methods Sometimes need to overwrite methods written in the superclass Re-define the method in the subclass.

30 Overriding Methods The Object class and tostring() tostring() = String output when you print the object

31 Overriding Methods The Object class and tostring() tostring() = String output when you print the object Object class has default tostring() method

32 The tostring() method public String tostring() { String str;... return str;

33 The tostring() method Circle circle1 = new Circle( Circle1, Red, 3.5); System.out.println(circle1); Output:?

34 The tostring() method Circle circle1 = new Circle( Circle1, Red, 3.5); System.out.println(circle1); Output: Circle@19821f

35 The tostring() method So override the method public String tostring() { String str = ; str += Circle: + getname() +, ; str += Color: + getcolor() +, ; str += Radius: + getradius(); return str;

36 The tostring() method Circle circle1 = new Circle( Circle1, Red, 3.5); System.out.println(circle1); Output: Circle: Circle1,Color: Red,Radius: 3.5

37 The equals() method The Object class and equals() equals (Object obj) = returns whether the object passed in and this one are the same

38 The equals() method Default implementation: public boolean equals(object obj) { return (this == obj); Compares references

39 The equals() method Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5);

40 The equals() method Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5); circle1.equals(circle2); //?

41 The equals() method Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5); circle1.equals(circle2); // returns false!

42 The equals() method Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5); circle1.equals(circle2); // returns false! Circle circle3 = circle1; circle1.equals(circle3); //?

43 The equals() method Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5); circle1.equals(circle2); // returns false! Circle circle3 = circle1; circle1.equals(circle3); // returns true!

44 The equals() method Override it public boolean equals(object obj) { if (obj instanceof Circle) return this.radius == ((Circle)obj).getRadius(); else return false;

45 The instanceof operator Test if object is of a specified type if (object instanceof Class)

46 The instanceof operator Test if object is of a specified type if (object instanceof Class) Example: Circle circle1 = new Circle(3.4); if (circle1 instanceof Circle) {

47 The instanceof operator Testing with the SuperClass: if (subclassobj instanceof SuperClass) {?

48 The instanceof operator Testing with the SuperClass: if (Circle instanceof GeometricObject) {?

49 The instanceof operator Testing with the SuperClass: if (subclassobj instanceof SuperClass) { returns true!

50 The instanceof operator Example: Circle circle1 = new Circle(3.4); if (circle1 instanceof GeometricObject) { returns true!

51 The instanceof operator What if we write: if (SuperClassObj instanceof subclass) {

52 The instanceof operator if (SuperClassObj instanceof subclass) { returns true as long as the object is an instance of the subclass

53 The instanceof operator Example: Object is a superclass of Circle public boolean equals(object obj) { if (obj instanceof Circle) return this.radius == ((Circle)obj).getRadius(); else return false;

54 The instanceof operator Be careful with null objects String s = null; s instanceof String = false

55 Object casting So we can determine if (SuperClassObj instanceof subclass) Convert it to subclass by casting

56 Object casting So we can determine if (SuperClassObj instanceof subclass) Convert it to subclass by casting Can now access members/properties

57 Object casting Once again... public boolean equals(object obj) { if (obj instanceof Circle) { return this.radius == ((Circle)obj).getRadius(); else return false;

58 Object casting Once again... public boolean equals(object obj) { if (obj instanceof Circle) { Circle circle = (Circle)obj; // casting return this.radius == circle.getradius(); else return false;

59 Overloading Methods Looked at it before. Can have multiple methods with the same name. But different parameters, return type

60 Dynamic binding Object type is determined at run-time ability of a program to resolve references to subclass methods at runtime.

61 Dynamic binding public class GeometricObject { protected String Color; protected String name; protected float area; public float getarea() { return -1;

62 Dynamic binding // Circle public class Circle { public float getarea() { return Math.PI * radius * radius; // Rectangle public class Rectangle { public float getarea() { return width * height;

63 Dynamic binding GeometricObject[] objs = new GeometricObject[2];

64 Dynamic binding GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2);

65 Dynamic binding GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2); for (GeometricObject i : objs) { System.out.println(i.getArea());

66 Dynamic binding GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2); for (GeometricObject i : objs) { System.out.println(i.getArea()); What s the output?

67 Dynamic binding Output: 9.0 2.0 The subclass getarea() methods are called. At run-time, Java dynamically determines the class.

68 final Classes Sometimes classes shouldn t be allowed to be extended. The String class for example. Use the keyword final final class String {...

69 final Methods Methods which can t be overridden in the subclasses.

70 Summary... Inheritance! super! protected Polymorphism! Overloading! Overwriting! Dynamic binding instanceof Object casting final classes and methods

71 Next lecture... Abstract Classes Interfaces