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

Similar documents
CS1150 Principles of Computer Science Objects and Classes

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

Inheritance and Polymorphism

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

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

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

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

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

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

C08: Inheritance and Polymorphism

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

Chapter 11 Inheritance and Polymorphism

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

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

C08: Inheritance and Polymorphism

Chapter 11 Inheritance and Polymorphism

Chapter 11 Inheritance and Polymorphism

We are on the GUI fast track path

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Inheritance (continued) Inheritance

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

CS171:Introduction to Computer Science II. Li Xiong

CS-202 Introduction to Object Oriented Programming

Introduction Programming Using Python Lecture 8. Dr. Zhang COSC 1437 Fall 2017 Nov 30, 2017

C18a: Abstract Class and Method

25. Generic Programming

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Inheritance (Deitel chapter 9)

CH. 2 OBJECT-ORIENTED PROGRAMMING

Inheritance and Polymorphism

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

ITI Introduction to Computing II

ITI Introduction to Computing II

CMSC 132: Object-Oriented Programming II. Inheritance

EKT472: Object Oriented Programming. Overloading and Overriding Method

Object Oriented Programming

Object Orientated Programming Details COMP360

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

C12a: The Object Superclass and Selected Methods

Practice for Chapter 11

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

Chapter 14 Abstract Classes and Interfaces

What is Inheritance?

Inheritance and Polymorphism

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

Chapter 6: Inheritance

Reusing Classes. Hendrik Speleers

Chapter 9 - Object-Oriented Programming: Inheritance

Inheritance Motivation

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

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

Programming 2. Inheritance & Polymorphism

Lecture 18 CSE11 Fall 2013 Inheritance

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

ECE 122. Engineering Problem Solving with Java

EECS 1001 and EECS 1030M, lab 01 conflict

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

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

Chapter 5 Object-Oriented Programming

Polymorphism and Inheritance

Java Object Oriented Design. CSC207 Fall 2014

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

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

C11: Garbage Collection and Constructors

Chapter 13 Abstract Classes and Interfaces

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

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

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

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

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

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

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

C10: Garbage Collection and Constructors

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

CSA 1019 Imperative and OO Programming

Making New instances of Classes

Inheritance and Encapsulation. Amit Gupta

Java Classes, Objects, Inheritance, Abstract and Interfaces Recap

CIS 110: Introduction to computer programming

CS Week 13. Jim Williams, PhD

Chapter 9 - Object-Oriented Programming: Polymorphism

Inheritance (an intuitive description)

Inheritance, Polymorphism, and Interfaces

Class, Variable, Constructor, Object, Method Questions

ITI Introduction to Computing II

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Classes and Inheritance Extending Classes, Chapter 5.2

8. Polymorphism and Inheritance

Chapter 13 Abstract Classes and Interfaces

index.pdf January 21,

Abstract Classes Interfaces

Week 5-1: ADT Design

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

C09: Interface and Abstract Class and Method

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

ITI Introduction to Computing II

ITI Introduction to Computing II

Transcription:

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

Outline Inheritance Superclass/supertype, subclass/subtype Inheritance and constructors in Java Inheritance and instance methods in Java The Object class in Java 9/20/2018 CUNY Brooklyn College 2

Class and Type A class defines a type, and often models a set of entities Example: to build a system for managing business at Brooklyn College, we consider People, a set of individuals (objects), modeled as a class that captures the essence of the set of objects People at Brooklyn College 2/26/2018 CUNY Brooklyn College 3

Subtypes Some people at Brooklyn are different from the others in some way Professors and students are subtypes of Brooklyn College People Professors Students People at Brooklyn College 2/26/2018 CUNY Brooklyn College 4

Type Hierarchy Characteristics and behavior What are Students and Professors in common? What are Students and Professors different? People at Brooklyn College Professors Students 2/26/2018 CUNY Brooklyn College 5

What s in Common? What characteristics (attributes) and behavior (actions) do People at Brooklyn College have in common? Characteristics (attributes, fields, or states): name, ID, address, email, phone, Behavior (actions, functions, or methods): change address, apply parking, 2/26/2018 CUNY Brooklyn College 6

What s Special? What s distinct about students? Characteristics (attributes, fields, or states): classes taken, tuition and fees, Behavior (actions, functions, or methods): add class, drop class, pay tuition, What s distinct about professors? Characteristics (attributes, fields, or states): course taught, rank, title, Behavior (actions, functions, or methods): register grade, apply promotion, 2/26/2018 CUNY Brooklyn College 7

Inheritance & Type Hierarchy A subtype (child) inherits characteristics (data fields & methods) and behavior (actions) of its super/base type (parent) - Class taught, - Register grades, People at Brooklyn College - Name, ID, address, phone, - Change address, apply parking Professors - Class taken, - Add class, Students 2/26/2018 CUNY Brooklyn College 8

Terms of Choice Terms Super type, Super class Base type, Base class Parent type, parent class Child type, child class Subtype, subclass In Java, we sometimes consider type and class are slightly different In Java, a pure abstract class is called an interface (to be discussed in the future) 2/26/2018 CUNY Brooklyn College 9

Example: Realizing the Type Hierarchy Classes: Person, Student, Professor - Class taught, - Register grades, People at Brooklyn College - Name, ID, address, phone, - Change address, apply parking Professors - Class taken, - Add class, Students 9/20/2018 CUNY Brooklyn College 10

Super Type (Super Class): Person public class Person { private String name; private String id; private String address; public Person(String name, String id, String address) { this.name = name; this.id = id; public void changeaddress(string address) { 2/26/2018 CUNY Brooklyn College 11

Subtype (Subclass): Student public class Student extends Person { public final static int MAX_NUM_COURSES = 10; private String[] classestaken; public Student(String name, String id, String address) {... // initializing inherited data fields classestaken = new String[MAX_NUM_COURSES]; public void havetakenclass(string classname) { public void showclassestaken() { 2/26/2018 CUNY Brooklyn College 12

Subtype (Subclass): Professor public class Professor extends Person { public final static int SABATTICAL_LEAVE_INTERVAL = 7; private int yearstarted; public Professor(String name, String id, String address, int yearstarted) {... // initializing inherited data fields this.yearstarted = yearstarted; public void applysabbatical(int applicationyear) { 2/26/2018 CUNY Brooklyn College 13

Questions Concepts Type, subtype, class, subclass Inheritance 2/26/2018 CUNY Brooklyn College 14

UML Diagram and Type Hierarchy UML diagram for showing class hierarchy Example: GeometricObject, Circle, Rectangle -color: String -filled: boolean GeometricObject -datecreated: java.util.date +GeometricObject() +GeometricObject(color: String, filled: boolean) +getcolor(): String +setcolor(color: String): void +isfilled(): boolean +setfilled(filled: boolean): void +getdatecreated(): java.util.date +tostring(): String -radius: double +Circle() Circle +Circle(radius: double) +Circle(radius: double, color: String, filled: boolean) +getradius(): double +setradius(radius: double): void +getarea(): double +getperimeter(): double +getdiameter(): double +printcircle(): void The color of the object (default: white). Indicates whether the object is filled with a color (default: false). The date when the object was created. Creates a GeometricObject. Creates a GeometricObject with the specified color and filled values. Returns the color. Sets a new color. Returns the filled property. Sets a new filled property. Returns the datecreated. Returns a string representation of this object. -width: double -height: double +Rectangle() Rectangle +Rectangle(width: double, height: double) +Rectangle(width: double, height: double color: String, filled: boolean) +getwidth(): double +setwidth(width: double): void +getheight(): double +setheight(height: double): void +getarea(): double +getperimeter(): double 9/20/2018 CUNY Brooklyn College 15

Constructors Let us consider Circle c = new Circle(); Are superclass s constructor inherited? No. They are not inherited. They are invoked explicitly or implicitly. Explicitly using the super keyword. 9/20/2018 CUNY Brooklyn College 16

Constructors Let us consider Circle c = new Circle(); Are superclass s Constructor Inherited? In other words, how are the data fields initialized? -color: String -filled: boolean GeometricObject -datecreated: java.util.date +GeometricObject() +GeometricObject(color: String, filled: boolean) +getcolor(): String +setcolor(color: String): void +isfilled(): boolean +setfilled(filled: boolean): void +getdatecreated(): java.util.date +tostring(): String -radius: double +Circle() Circle +Circle(radius: double) +Circle(radius: double, color: String, filled: boolean) +getradius(): double +setradius(radius: double): void +getarea(): double +getperimeter(): double +getdiameter(): double +printcircle(): void The color of the object (default: white). Indicates whether the object is filled with a color (default: false). The date when the object was created. Creates a GeometricObject. Creates a GeometricObject with the specified color and filled values. Returns the color. Sets a new color. Returns the filled property. Sets a new filled property. Returns the datecreated. Returns a string representation of this object. -width: double -height: double +Rectangle() Rectangle +Rectangle(width: double, height: double) +Rectangle(width: double, height: double color: String, filled: boolean) +getwidth(): double +setwidth(width: double): void +getheight(): double +setheight(height: double): void +getarea(): double +getperimeter(): double 9/20/2018 CUNY Brooklyn College 17

Constructors in Super- and Sub- Classes Are superclass s Constructor Inherited? No. They are not inherited, but one is always invoked They are invoked explicitly or implicitly. Explicitly using the super keyword Implicitly the superclass's no-arg constructor is automatically invoked if the keyword super is not explicitly used. 9/20/2018 CUNY Brooklyn College 18

Implicit Invocation of Superclass s Constructor A superclass s constructor is always invoked even if it isn t invoked explicitly using super. Which constructor is invoked implicitly? public A() { is equivalent to public A() { super(); public A(double d) { // some statements is equivalent to public A(double d) { super(); // some statements 9/20/2018 CUNY Brooklyn College 19

Explicit Invocation of Superclass s Methods super refers to the superclass Use it To call a superclass constructor Java requires that the statement that uses the keyword super appear first in the constructor. To call a superclass method 9/20/2018 CUNY Brooklyn College 20

Constructor Chaining Invocation of superclass s constructor (along the inheritance chain) Example Consider classes: Person, Employee, Faculty Person Employee Faculty 9/20/2018 CUNY Brooklyn College 21

Constructor Chaining: Example class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); class Employee(String s) { System.out.println(s); class Faculty extends Employee { public static void main(string[] args) { new Faculty(); public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); 9/20/2018 CUNY Brooklyn College 22

Discussion: No-Arg Constructor Is there an error in the code below, and why? public class Apple extends Fruit { public class Fruit { public Fruit(String name) { System.out.println("Fruit's constructor is invoked"); 9/20/2018 CUNY Brooklyn College 23

Questions? Constructors in superclass Explicit and implicit invocation Constructor chaining 9/20/2018 CUNY Brooklyn College 24

Defining a Subclass A subclass inherits from a superclass. One can also: Add new properties Add new methods Override the methods of the superclass 9/20/2018 CUNY Brooklyn College 25

Overriding Methods in Superclass Modify the implementation of a method defined in the superclass public class Circle extends GeometricObject { // Other methods are omitted /** Override the tostring method defined in GeometricObject */ public String tostring() { return super.tostring() + "\nradius is " + radius; 9/20/2018 CUNY Brooklyn College 26

Invoking Superclass s Instance Method Example One could rewrite the printcircle() method in the Circle class as follows: public void printcircle() { System.out.println("The circle is created " + super.getdatecreated() + " and the radius is " + radius); 9/20/2018 CUNY Brooklyn College 27

Discussion: Method Overriding Can you override a private method in the superclass? 9/20/2018 CUNY Brooklyn College 28

Discussion: Method Overriding Can you override a private method in the superclass? No An instance method can be overridden only if it is accessible. A private method is not accessible outside its own class. A private method in the superclass can only be accessible in the superclass itself, is inaccessible in the subclass. Thus a private method cannot be overridden. 9/20/2018 CUNY Brooklyn College 29

Discussion: Unrelated Methods Can you have a method whose signature is identical to a private method in the superclass? 9/20/2018 CUNY Brooklyn College 30

Discussion: Unrelated Methods Can you have a method whose signature is identical to a private method in the superclass? Yes However, this isn t method overriding. The two methods are unrelated, but happen to have the identical name. 9/20/2018 CUNY Brooklyn College 31

Discussion: Static Method Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. 9/20/2018 CUNY Brooklyn College 32

Overriding vs. Overloading Overriding is to redefine the method with the identical signature in the superclass public class Test { public static void main(string[] args) { A a = new A(); a.p(10); a.p(10.0); public class Test { public static void main(string[] args) { A a = new A(); a.p(10); a.p(10.0); class B { public void p(double i) { System.out.println(i * 2); class B { public void p(double i) { System.out.println(i * 2); class A extends B { // This method overrides the method in B public void p(double i) { System.out.println(i); class A extends B { // This method overloads the method in B public void p(int i) { System.out.println(i); Two methods with identical name but different signature 9/20/2018 CUNY Brooklyn College 33

Questions? Defining subclasses A few topics Invoking superclass s methods (constructors and instance methods) Overriding Overriding and overloading 9/20/2018 CUNY Brooklyn College 34

Codelab Exercise Codelab exercises marked as due by 10/04/2018 9/20/2018 CUNY Brooklyn College 35

In-Class Exercise C09a-1 Listings 11.1-11.3 in the textbook define 3 classes: GeometricObject, Circle, and Rectangle. In this exercise you are to add two classes to the hierarchy, Triangle and EquilateralTriangle, and write a client class to use the Triangle and EquilateralTriangle classes. Create a C09a-1 directory in the directory that hosts your weekly programming repository, work at the directory The Triangle class is a subclass to GeometricObject, and the EquilateralTriangle isa subclass to Triangle. An EquilateralTriangle is a triangles whose sides are equal. Your submission should include 6 files (6 classes): GeometricObject.java, Circle.java, Rectangle.java, Triangle.java, EquilateralTriangle, and TriangleClient.java Use git to make a submission 9/20/2018 CUNY Brooklyn College 36