ITI Introduction to Computing II

Similar documents
ITI Introduction to Computing II

ITI Introduction to Computing II

ITI Introduction to Computing II

index.pdf January 21,

ITI Introduction to Computing II

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

Java Object Oriented Design. CSC207 Fall 2014

CS-202 Introduction to Object Oriented Programming

Computer Science 210: Data Structures

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

Reviewing OO Concepts

Inheritance and Polymorphism

Reusing Classes. Hendrik Speleers

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

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

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

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

CS1150 Principles of Computer Science Objects and Classes

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

PIC 20A Number, Autoboxing, and Unboxing

What is Inheritance?

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

Making New instances of Classes

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

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

Programming II (CS300)

Reviewing OO Concepts

Abstract Classes Interfaces

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

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

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

Advanced Placement Computer Science. Inheritance and Polymorphism

Programming Languages and Techniques (CIS120)

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

Data Structures (list, dictionary, tuples, sets, strings)

Inheritance and Interfaces

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

Programming in C# Inheritance and Polymorphism

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

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

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

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

Introduction to Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Chapter 5 Object-Oriented Programming

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

Inheritance Motivation

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS1004: Intro to CS in Java, Spring 2005

Programming II (CS300)

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Methods Common to all Classes

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

CMSC 132: Object-Oriented Programming II

Inheritance, and Polymorphism.

JAVA MOCK TEST JAVA MOCK TEST II

OBJECT ORİENTATİON ENCAPSULATİON

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Programming Languages and Techniques (CIS120)

CS111: PROGRAMMING LANGUAGE II

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

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

Chapter 9 Abstract Classes and Interfaces

Programming Languages and Techniques (CIS120)

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Chapter 6: Inheritance

ECE 122. Engineering Problem Solving with Java

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Inheritance. Transitivity

Inheritance -- Introduction

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

PROGRAMMING LANGUAGE 2

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Object-Oriented Programming Concepts

Inheritance, Polymorphism, and Interfaces

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

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

Lecture 7: Classes and Objects CS2301

CMSC 132: Object-Oriented Programming II

CS111: PROGRAMMING LANGUAGE II

Programming Languages and Techniques (CIS120)

Lecture 18 CSE11 Fall 2013 Inheritance

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Chapter 10 Classes Continued. Fundamentals of Java

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

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

3.1 Class Declaration

Inheritance (Deitel chapter 9)

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

INHERITANCE AND OBJECTS. Fundamentals of Computer Science I

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

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

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

COMP 110/L Lecture 19. Kyle Dewey

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

Transcription:

ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.

Summary We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.

Summary We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems. The data, and the methods that act upon the data, are encapsulated into a single entity called the object.

Summary We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems. The data, and the methods that act upon the data, are encapsulated into a single entity called the object. The instance variables define the properties or state of an object.

Summary We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems. The data, and the methods that act upon the data, are encapsulated into a single entity called the object. The instance variables define the properties or state of an object. In particular, we have seen that OOP provides mechanisms to control the visibility of the methods and the variables.

Summary We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems. The data, and the methods that act upon the data, are encapsulated into a single entity called the object. The instance variables define the properties or state of an object. In particular, we have seen that OOP provides mechanisms to control the visibility of the methods and the variables. The methods and variables that are public define the interface of the object.

Summary We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems. The data, and the methods that act upon the data, are encapsulated into a single entity called the object. The instance variables define the properties or state of an object. In particular, we have seen that OOP provides mechanisms to control the visibility of the methods and the variables. The methods and variables that are public define the interface of the object. Having the interface clearly defined allows the implementers and the users of the class to work independently; the creator can change the implementation of the class, as long as it does not affect the interface, and the programs developed by the users will continue to work.

Summary We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems. The data, and the methods that act upon the data, are encapsulated into a single entity called the object. The instance variables define the properties or state of an object. In particular, we have seen that OOP provides mechanisms to control the visibility of the methods and the variables. The methods and variables that are public define the interface of the object. Having the interface clearly defined allows the implementers and the users of the class to work independently; the creator can change the implementation of the class, as long as it does not affect the interface, and the programs developed by the users will continue to work. As a general principle, in CS II, all the instance variables should be declared private.

Summary We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems. The data, and the methods that act upon the data, are encapsulated into a single entity called the object. The instance variables define the properties or state of an object. In particular, we have seen that OOP provides mechanisms to control the visibility of the methods and the variables. The methods and variables that are public define the interface of the object. Having the interface clearly defined allows the implementers and the users of the class to work independently; the creator can change the implementation of the class, as long as it does not affect the interface, and the programs developed by the users will continue to work. As a general principle, in CS II, all the instance variables should be declared private.

If the value of a variable needs to be accessed (read or mutated) from outside the class, then the interface of the object will include setter and getter methods. This principle will allow us to maintain the integrity of the objects.

If the value of a variable needs to be accessed (read or mutated) from outside the class, then the interface of the object will include setter and getter methods. This principle will allow us to maintain the integrity of the objects. The class specifies the content of the objects but it also exists during the execution of a program. Each object knows the class from which it was instantiated from (is an instance of). No matter how many instances there are, 0, 1 or n, there is only one copy of the class.

If the value of a variable needs to be accessed (read or mutated) from outside the class, then the interface of the object will include setter and getter methods. This principle will allow us to maintain the integrity of the objects. The class specifies the content of the objects but it also exists during the execution of a program. Each object knows the class from which it was instantiated from (is an instance of). No matter how many instances there are, 0, 1 or n, there is only one copy of the class. Class variables and methods are shared by all instances of a class.

If the value of a variable needs to be accessed (read or mutated) from outside the class, then the interface of the object will include setter and getter methods. This principle will allow us to maintain the integrity of the objects. The class specifies the content of the objects but it also exists during the execution of a program. Each object knows the class from which it was instantiated from (is an instance of). No matter how many instances there are, 0, 1 or n, there is only one copy of the class. Class variables and methods are shared by all instances of a class. In today s lecture, we look at other important features of object-oriented programming that help organizing and maintaining large software systems: inheritance and polymorphism.

Inheritance OO languages, in general, also offer other tools to structure large systems. Inheritance is one of them.

Inheritance OO languages, in general, also offer other tools to structure large systems. Inheritance is one of them. Inheritance allows to organize the classes hierarchically.

Inheritance OO languages, in general, also offer other tools to structure large systems. Inheritance is one of them. Inheritance allows to organize the classes hierarchically. Inheritance favors code reuse!

Inheritance The class immediately above is called the superclass or parent class while the class immediately below is called the subclass, child class or derived class. Bird Pigeon is a

Inheritance The class immediately above is called the superclass or parent class while the class immediately below is called the subclass, child class or derived class. Bird Pigeon is a In this example, Bird is the superclass of Pigeon, i.e. Pigeon is a subclass of Bird.

Inheritance Bird Pigeon is a In Java, the is a relationship is expressed using the reserved keyword extends, as follows: public class Pigeon extends Bird {...

Inheritance Bird Pigeon is a In UML, the is a relationship is expressed using a continuous line connecting the child to its parent, and an open triangle pointing towards the parent.

Inheritance In Java, the classes are organized into a single hierarchy, with the most general class, called Object, being at the top (or root) of the tree.

Object #clone(): Object +equals(object:obj): boolean +getclass(): Class +tostring(): String Number +bytevalue(): byte +doublevalue(): double +floatvalue(): float +intvalue(): int +longvalue(): long +shortvalue() Double +MAX_VALUE: double +MIN_VALUE: double +bytevalue(): byte +doublevalue(): double +floatvalue(): float +intvalue(): int +longvalue(): long +shortvalue() +compareto(d:double): int +parsedouble(s:string): double +tostring(): String Integer +MAX_VALUE: int +MIN_VALUE: int +bytevalue(): byte +doublevalue(): double +floatvalue(): float +intvalue(): int +longvalue(): long +shortvalue() +compareto(i:integer): int +parseint(s:string): int +tostring(): String

Inheritance If the superclass is not explicitly mentioned, Object is the immediate parent class, the following two declarations are therefore identical public class C {... and public class C extends Object {...

Inheritance In Java, all the classes have exactly one parent; except Object that has no parent.

Inheritance In Java, all the classes have exactly one parent; except Object that has no parent. We talk about single inheritance as opposed to multiple inheritance.

What does it mean? A class inherits all the characteristics (variables and methods) of its superclass(es).

What does it mean? A class inherits all the characteristics (variables and methods) of its superclass(es). 1. a subclass inherits all the methods and variables of its superclass(es);

What does it mean? A class inherits all the characteristics (variables and methods) of its superclass(es). 1. a subclass inherits all the methods and variables of its superclass(es); 2. a subclass can introduce/add new methods and variables;

What does it mean? A class inherits all the characteristics (variables and methods) of its superclass(es). 1. a subclass inherits all the methods and variables of its superclass(es); 2. a subclass can introduce/add new methods and variables; 3. a subclass can override the methods of its superclass.

What does it mean? A class inherits all the characteristics (variables and methods) of its superclass(es). 1. a subclass inherits all the methods and variables of its superclass(es); 2. a subclass can introduce/add new methods and variables; 3. a subclass can override the methods of its superclass. Because of 2 and 3, the subclass is a specialization of the superclass, i.e. the superclass is more general than its subclasses.

Inheritance Inheritance is one of the tools that help developing reusable components (classes).

Shape Variants of this example can be found in most textbooks about object-oriented programming.

Shape Variants of this example can be found in most textbooks about object-oriented programming. Problem: A software system must be developed to represent various shapes, such as circles and rectangles.

Shape Variants of this example can be found in most textbooks about object-oriented programming. Problem: A software system must be developed to represent various shapes, such as circles and rectangles. All the shapes must have two instance variables, x and y, to represent the location of each object.

Shape Shape -x: double -y: double Circle Rectangle

Shape Furthermore, all the shapes should have the following methods: double getx(); double gety(); void moveto(double x, double y); double area(); void scale(double factor); String tostring(); // Returns the value of x // Returns the value of y // Move the shape to a new location // Calculates the area of the shape // Scales the shape by some factor // Returns a String representation

Shape Furthermore, all the shapes should have the following methods: double getx(); double gety(); void moveto(double x, double y); double area(); void scale(double factor); String tostring(); // Returns the value of x // Returns the value of y // Move the shape to a new location // Calculates the area of the shape // Scales the shape by some factor // Returns a String representation Keep the specification in mind as we won t be able to implement it fully, at first.

Shape The implementation of the first three methods would be the same for all kinds of shapes.

Shape Shape -x: double -y: double + getx() : double + gety() : double + moveto( x: double, y: double ) : void Circle Rectangle

Shape On the other hand, the calculation of the area and the implementation of the scale method would depend on the kind of shape being dealt with.

Shape On the other hand, the calculation of the area and the implementation of the scale method would depend on the kind of shape being dealt with. Finally, the method tostring() requires information from both levels, general and specific, all shapes should display their location and also their specific information, such as the radius in the case of a circle.

Shape public class Shape extends Object { private double x; private double y; public Shape() { x = 0; y = 0;

Shape public class Shape extends Object { private double x; private double y; public Shape() { x = 0; y = 0; public Shape( double x, double y ) { this.x = x; this.y = y; Can I do this?

Shape public class Shape extends Object { private double x; private double y; public Shape() { x = 0; y = 0; public Shape( double x, double y ) { this.x = x; this.y = y; Can I do this? Yes. Several methods (or constructors) with the same name can be added to a class, as long as their signature differ.

Shape public class Shape extends Object { private double x; private double y; public Shape() { x = 0; y = 0; public Shape( double x, double y ) { this.x = x; this.y = y; Can I do this? Yes. Several methods (or constructors) with the same name can be added to a class, as long as their signature differ. I am calling this ad hoc polymorphism, or overloading.

Shape public class Shape extends Object { private double x; private double y; public Shape() { x = 0; y = 0; public Shape( double x, double y ) { this.x = x; this.y = y; Can I do this? Yes. Several methods (or constructors) with the same name can be added to a class, as long as their signature differ. I am calling this ad hoc polymorphism, or overloading. Why would you want to do this?

Shape public class Shape extends Object { private double x; private double y; public double getx() { return x; public double gety() { return y; Adding the getters!

Shape public class Shape extends Object { private double x; private double y; public final double getx() { return x; public final double gety() { return y; By using the keyword final, we can prevent the descendants of this class overriding the method.

Shape public class Shape extends Object { private double x; private double y; public final double getx() { return x; public final double gety() { return y; public final void moveto( double x, double y ) { this.x = x; this.y = y; The method moveto can be seen as a setter!

Circle public class Circle extends Shape { The above declaration defines a class Circle that extends Shape, which means that an instance of the class Circle possesses two instance variables x and y, as well as the following methods: getx(), gety() and moveto(double x, double).

Circle public class Circle extends Shape { // Instance variable private double radius; The instance variables x and y and inherited (common to all Shapes). variable radius is specific to a Circle. The

Private vs protected With the current definition of the class Shape, it would not have been possible to define the constructor of the class Circle as follows: public Circle( double x, double y, double radius ) { this.x = x; this.y = y; this.radius = radius; The compiler would complain saying x has private access in Shape (and similarly for y).

Private vs protected With the current definition of the class Shape, it would not have been possible to define the constructor of the class Circle as follows: public Circle( double x, double y, double radius ) { this.x = x; this.y = y; this.radius = radius; The compiler would complain saying x has private access in Shape (and similarly for y). This is because an attribute declared private in the parent class cannot be accessed within the child class.

Private vs protected With the current definition of the class Shape, it would not have been possible to define the constructor of the class Circle as follows: public Circle( double x, double y, double radius ) { this.x = x; this.y = y; this.radius = radius; The compiler would complain saying x has private access in Shape (and similarly for y). This is because an attribute declared private in the parent class cannot be accessed within the child class.

Private vs protected To circumvent this and implement the constructor as above, the definition of Shape should be modified so that x and y would be declared protected: public class Shape extends Object { protected double x; protected double y;...

Private vs protected To circumvent this and implement the constructor as above, the definition of Shape should be modified so that x and y would be declared protected: public class Shape extends Object { protected double x; protected double y;...

Private vs protected When possible, it is preferable to maintain the visibility private.

Private vs protected When possible, it is preferable to maintain the visibility private. Private instance variables and final instance methods go hand in hand.

Private vs protected When possible, it is preferable to maintain the visibility private. Private instance variables and final instance methods go hand in hand. The declaration of an instance variable private prevents the subclasses from accessing the variable.

Private vs protected When possible, it is preferable to maintain the visibility private. Private instance variables and final instance methods go hand in hand. The declaration of an instance variable private prevents the subclasses from accessing the variable. The declaration of a method final prevents subclasses from overriding the method.

Private vs protected When possible, it is preferable to maintain the visibility private. Private instance variables and final instance methods go hand in hand. The declaration of an instance variable private prevents the subclasses from accessing the variable. The declaration of a method final prevents subclasses from overriding the method.

Private vs protected By declaring the instance variables private and the access/mutator instance methods final you ensure that all the modifications to the instance variables are concentrated in the class where they were first declared.

Circle public class Circle extends Shape { private double radius; // Constructors public Circle() { super(); radius = 0; public Circle( double x, double y, double radius ) { super( x, y ); this.radius = radius;

super() The statement super(... ) is an explicit call to the constructor of the immediate superclass.

super() The statement super(... ) is an explicit call to the constructor of the immediate superclass. This particular construction can only appear in a constructor;

super() The statement super(... ) is an explicit call to the constructor of the immediate superclass. This particular construction can only appear in a constructor; Can only be the first statement of the constructor;

super() The statement super(... ) is an explicit call to the constructor of the immediate superclass. This particular construction can only appear in a constructor; Can only be the first statement of the constructor; The super() will be automatically inserted for you unless you insert a super(... ) yourself!?

super() The super() will be automatically inserted for you unless you insert a super(... ) yourself!?

super() The super() will be automatically inserted for you unless you insert a super(... ) yourself!? If the first statement of a constructor is not an explicit call super(... ), Java inserts a call super(), which means that the superclass has to have a constructor of arity 0, or else a compile time error will occur.

super() The super() will be automatically inserted for you unless you insert a super(... ) yourself!? If the first statement of a constructor is not an explicit call super(... ), Java inserts a call super(), which means that the superclass has to have a constructor of arity 0, or else a compile time error will occur. Remember, the default constructor, the one with arity 0, is no longer present if a constructor has been defined.

super() If a constructor body does not begin with an explicit constructor invocation (... ), then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation super();, an invocation of the constructor of its direct superclass that takes no arguments. Gosling et al. (2000) The Java Language Specification.

Circle public class Circle extends Shape { private double radius; // Access method public double getradius() { return radius;

Rectangle public class Rectangle extends Shape { private double width; private double height; public Rectangle() { super(); width = 0; height = 0; public Rectangle( double x, double y, double width, double height ) super(x, y); this.width = width; this.height = height;

Rectangle public class Rectangle extends Shape { private double width; private double height; //... public double getwidth() { return width; public double getheight() { return height;

Rectangle public class Rectangle extends Shape { private double width; private double height; //... public void flip() { double tmp = width; width = height; height = tmp;

Circle d = new Circle( 100, 200, 10 ); System.out.println( d.getradius() ); Circle c = new Circle(); System.out.println( c.getx() ); d.scale( 2 ); System.out.println ( d ); Rectangle r = new Rectangle(); System.out.println( r.getwith() ); Rectangle s = new Rectangle( 50, 50, 10, 15 ); System.out.println( s.gety() ); s.flip(); System.out.println( s.gety() );

Summary Inheritance allows to reuse code. The methods getx(), gety() and moveto() were only defined in the class Shape. Fixing a bug or making an improvement in the superclass will fix or improve all the subclasses.