Chapter 6: Inheritance

Similar documents
EECS 1001 and EECS 1030M, lab 01 conflict

COMP 249: Object Oriented Programming II. Tutorial 2: Intro to Inheritance

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

Implementing non-static features

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

ITI Introduction to Computing II

S.O.L.I.D: Software Engineering Principles

For this section, we will implement a class with only non-static features, that represents a rectangle

CS1150 Principles of Computer Science Objects and Classes

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

ITI Introduction to Computing II

Computer Science 210: Data Structures

Reviewing OO Concepts

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

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

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

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

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

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

Inheritance (Deitel chapter 9)

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

Chapter 9 - Object-Oriented Programming: Polymorphism

Introduction to Object-Oriented Programming

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

COMP 249: Object Oriented Programming II. Tutorial 11: Generics

Inheritance, and Polymorphism.

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Inheritance. Transitivity

We are on the GUI fast track path

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

Advanced Placement Computer Science. Inheritance and Polymorphism

Design Principles: Part 2

Design Principles: Part 2

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.

Liskov Substitution Principle

Objects and Classes (1)

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. Lecture 11 COP 3252 Summer May 25, 2017

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

Programming in the large

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

ITI Introduction to Computing II

INHERITANCE. Spring 2019

Reusing Classes. Hendrik Speleers

Inheritance and Encapsulation. Amit Gupta

Programming 2. Inheritance & Polymorphism

CS-202 Introduction to Object Oriented Programming

Inheritance and Polymorphism

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

CS 251 Intermediate Programming Inheritance

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

COMP 401 INHERITANCE: IS-A. Instructor: Prasun Dewan

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

Chapter 13 Abstract Classes and Interfaces

Reviewing OO Concepts

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

The Nervous Shapes Example

Chapter 11 Inheritance and Polymorphism

Rectangle. Lecture 2 week 2 - Objects & Classes Exercise 1. A. Write a driver class code in java depending on the following UML:

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

2009 Academic Challenge

Chapter 14 Abstract Classes and Interfaces

TWO-DIMENSIONAL FIGURES

Everything is an object. Almost, but all objects are of type Object!

AShape.java Sun Jan 21 22:32: /** * Abstract strategy to compute the area of a geometrical shape. Dung X. Nguyen * * Copyright

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

Introductory Programming Inheritance, sections

First Name: AITI 2004: Exam 2 July 19, 2004

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Binghamton University. CS-140 Fall Chapter 9. Inheritance

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

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

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

Chapter 9 - Object-Oriented Programming: Inheritance

CPSC 410? Advanced Software Engineering Mid-term Examination (Term I ) SOLUTION Instructor: Gail Murphy

Java Comparable interface

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

Inheritance and Polymorphism

Classes as Blueprints: How to Define New Types of Objects

WYSE Academic Challenge Computer Science Test (State) 2014 Solution Set

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

Extending Classes (contd.) (Chapter 15) Questions:

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

Inheritance (an intuitive description)

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

CMP-326 Exam 2 Spring 2018 Total 120 Points Version 1

Chapter 10 Classes Continued. Fundamentals of Java

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

COMP 250. inheritance (cont.) interfaces abstract classes

CS-140 Fall 2018 Test 2 Version A Nov. 12, Name:

Object-Oriented Programming

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Inheritance (Part 5) Odds and ends

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

Transcription:

Chapter 6: Inheritance EECS 1030 moodle.yorku.ca

State of an object final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); Problem Draw the memory diagram.

Memory diagram final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); 100 main invocation WIDTH HEIGHT WEIGHT rectangle

Memory diagram final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); 100 main invocation 3 WIDTH HEIGHT WEIGHT rectangle

Memory diagram final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); 100 main invocation 3 WIDTH 4 HEIGHT WEIGHT rectangle

Memory diagram final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle

Memory diagram final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80 GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle 200 GoldenRectangle object width height weight

Memory diagram final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80 GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle 200 GoldenRectangle object 3 width 4 height 80 weight

Memory diagram final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80 GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT 200 rectangle 200 GoldenRectangle object 3 width 4 height 80 weight

State of an object Note The private attributes width and height of the Rectangle class are part of the state of a GoldenRectangle object, but are not inherited. As a result, the private attributes width and height of the Rectangle class cannot be accessed by their name in the GoldenRectangle class.

The extends keyword To specify that the GoldenRectangle class is a subclass of the Rectangle class, we use the following class header: public class GoldenRectangle extends Rectangle

Attributes Question Which attributes do we introduce in the GoldenRectangle class? Provide names and types.

Attributes Question Which attributes do we introduce in the GoldenRectangle class? Provide names and types. Answer private int weight;

Attributes Question Which attributes do we introduce in the GoldenRectangle class? Provide names and types. Answer private int weight; Note We do not introduce attributes width and height in the GoldenRectangle class, since they are already present in the super class Rectangle.

Constructors Question Where do we initialize the state of an object?

Constructors Question Where do we initialize the state of an object? Answer In the constructors.

Constructors Question Where do we initialize the state of an object? Answer In the constructors. Question Which attributes are part of the state of a GoldenRectangle object?

Constructors Question Where do we initialize the state of an object? Answer In the constructors. Question Which attributes are part of the state of a GoldenRectangle object? Answer width, height and weight.

Constructors Problem In the constructors of the GoldenRectangle class, we have to initialize the attributes width, height and weight. However, we cannot access width and height by their name, that is, cannot use this.width and this.height in the constructors of the GoldenRectangle class.

Constructors Problem In the constructors of the GoldenRectangle class, we have to initialize the attributes width, height and weight. However, we cannot access width and height by their name, that is, cannot use this.width and this.height in the constructors of the GoldenRectangle class. Solution Delegate to a constructor of the Rectangle class.

Memory diagram final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80 GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle 200 GoldenRectangle object width height weight

Memory diagram final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80 GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle 200 GoldenRectangle object width height weight 300 GoldenRectangle invocation 200 this 3 width 4 height 80 weight

Memory diagram final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80 GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT); 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle 200 GoldenRectangle object width height weight 300 GoldenRectangle invocation 200 this 3 width 4 height 80 weight 400 Rectangle invocation 200 this 3 width 4 height

Memory diagram this.width = width; this. height = height; 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle 200 GoldenRectangle object width height weight 300 GoldenRectangle invocation 200 this 3 width 4 height 80 weight 400 Rectangle invocation 200 this 3 width 4 height

Memory diagram this.width = width; this. height = height; 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle 200 GoldenRectangle object 3 width height weight 300 GoldenRectangle invocation 200 this 3 width 4 height 80 weight 400 Rectangle invocation 200 this 3 width 4 height

Memory diagram this.width = width; this.height = height; 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle 200 GoldenRectangle object 3 width 4 height weight 300 GoldenRectangle invocation 200 this 3 width 4 height 80 weight 400 Rectangle invocation 200 this 3 width 4 height

Memory diagram... this.weight = weight; 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle 200 GoldenRectangle object 3 width 4 height weight 300 GoldenRectangle invocation 200 this 3 width 4 height 80 weight

Memory diagram... this.weight = weight; 100 main invocation 3 WIDTH 4 HEIGHT 80 WEIGHT rectangle 200 GoldenRectangle object 3 width 4 height 80 weight 300 GoldenRectangle invocation 200 this 3 width 4 height 80 weight

Delegation Question How do we delegate in the constructor of the GoldenRectangle class to the constructor of the Rectangle class?

Delegation Question How do we delegate in the constructor of the GoldenRectangle class to the constructor of the Rectangle class? Answer Although it may not be the most intuitive syntax, we use super(width, height);

Delegation Question How do we delegate in the constructor of the GoldenRectangle class to the constructor of the Rectangle class? Answer Although it may not be the most intuitive syntax, we use super(width, height); Note super has an implicit parameter, namely this.

Delegation Question How do we delegate in the constructor of the GoldenRectangle class to the constructor of the Rectangle class? Answer Although it may not be the most intuitive syntax, we use super(width, height); Note super has an implicit parameter, namely this. Rule If we delegate to the constructor of the super class, then this has to be done first, that is, super needs to be the first statement of the constructor.

Delegation Problem Implement the constructor of the GoldenRectangle class.

Methods Question Which methods are inherited by the GoldenRectangle class from the Rectangle class?

Methods Question Which methods are inherited by the GoldenRectangle class from the Rectangle class? Answer compareto, getarea, getheight, getwidth, scale, setheight and setwidth.

Methods Question Which methods are inherited by the GoldenRectangle class from the Rectangle class? Answer compareto, getarea, getheight, getwidth, scale, setheight and setwidth. Question Which methods are overridden of the Rectangle class in the GoldenRectangle class?

Methods Question Which methods are inherited by the GoldenRectangle class from the Rectangle class? Answer compareto, getarea, getheight, getwidth, scale, setheight and setwidth. Question Which methods are overridden of the Rectangle class in the GoldenRectangle class? Answer equals, hashcode and tostring.

Methods Question Which methods of the GoldenRectangle class are new?

Methods Question Which methods of the GoldenRectangle class are new? Answer getweight.

Inherited methods Nothing needs to be done.

New methods Problem Implement getweight method.

Overridden methods Problem Implement equals, hashcode and tostring.