Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Similar documents
Chapter 9 - Object-Oriented Programming: Polymorphism

Chapter 9 - Object-Oriented Programming: Inheritance

Inheritance (Deitel chapter 9)

Inheritance Motivation

Part 11 Object Oriented Programming: Inheritance

ITI Introduction to Computing II

ITI Introduction to Computing II

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

Java Object Oriented Design. CSC207 Fall 2014

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Making New instances of Classes

Chapter 20 - C++ Virtual Functions and Polymorphism

Polymorphism (Deitel chapter 10) (Old versions: chapter 9)

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

Reusing Classes. Hendrik Speleers

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

index.pdf January 21,

OBJECT ORİENTATİON ENCAPSULATİON

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

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

CS1150 Principles of Computer Science Objects and Classes

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

ITI Introduction to Computing II

ITI Introduction to Computing II

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

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

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

Programming Languages and Techniques (CIS120)

Inheritance and Polymorphism

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

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

CS 251 Intermediate Programming Inheritance

Chapter 14 Abstract Classes and Interfaces

Chapter 5. Inheritance

Java Programming Lecture 7

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Programming Languages and Techniques (CIS120)

Chapter 6: Inheritance

CS-202 Introduction to Object Oriented Programming

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

Chapter 10 Classes Continued. Fundamentals of Java

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object-Oriented Programming: Polymorphism

What is Inheritance?

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

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

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

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

C++ Important Questions with Answers

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

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

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

ITI Introduction to Computing II

Polymorphism. Polymorphism. CSC 330 Object Oriented Programming. What is Polymorphism? Why polymorphism? Class-Object to Base-Class.

COMP 110/L Lecture 19. Kyle Dewey

CST141--Inheritance 2

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

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

Polymorphism and Interfaces. CGS 3416 Spring 2018

Introduction. Introduction. Introduction

Chapter 19 - C++ Inheritance

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

Inheritance and Polymorphism

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

Topic 7: Algebraic Data Types

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

Inheritance and Interfaces

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

Inheritance & Polymorphism

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Inheritance, Polymorphism, and Interfaces

Programming 2. Inheritance & Polymorphism

Programming Languages and Techniques (CIS120)

Instance Members and Static Members

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance and Polymorphism

Advanced Placement Computer Science. Inheritance and Polymorphism

CSC330 Object Oriented Programming. Inheritance

Inheritance (continued) Inheritance

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Inheritance (Outsource: )

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

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

More Relationships Between Classes

Computer Science 210: Data Structures

CS111: PROGRAMMING LANGUAGE II

CMSC 132: Object-Oriented Programming II

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

COURSE 2 DESIGN PATTERNS

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

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

core Advanced Object-Oriented Programming in Java

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

CST242 Object-Oriented Programming Page 1

Transcription:

Inheritance & Polymorphism Recap Inheritance & Polymorphism 1

Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object, thus reusing its code.! The class inheriting is called a subclass (or derived class).! The class inherited from is called a superclass (or base class). Inheritance & Polymorphism 2

Inheritance & Polymorphism! Inheritance is a form of software reusability in which new classes are created from existing classes by n absorbing their attributes and behaviours and n enhance these, with capabilities the new classes require.! Polymorphism n enables us to write programs (and methods) in a general fashion to handle a wide variety of existing and yet-to-bespecified related classes. n makes it easy to add new capabilities to a system.! Inheritance and Polymorphism are effective techniques for dealing with software complexity. Inheritance & Polymorphism 3

Case Study - Point,Circle,Cylinder! Suppose a set of shape classes such as Circle, Triangle, Square etc. are all derived from superclass Shape, and each class has the ability to draw itself (has its own draw() method,which would be different in each case).! When drawing a shape, whatever shape it might be, it would be nice to be able to treat all these shapes generically as objects of the superclass Shape.! Then to draw any shape, we could call the draw() method of superclass Shape and let the program determine dynamically (at run time) which subclass draw() method should be called (depending on the objects type).! To enable this kind of behaviour, we declare draw() in the superclass, and override draw() in each of the subclasses to draw the appropriate shape. Inheritance & Polymorphism 4

The abstract keyword! An abstract method is not actually implemented in the class. The body of the method is implemented in subclasses of that class. public abstract void draw();! An abstract method must be part of an abstract class. public abstract class Shape extends Object! Abstract classes cannot be instantiated. It is a compile-time error to try something like Shape m = new Shape(); where Shape has been declared to be abstract Inheritance & Polymorphism 5

Abstract Classes! A variable of an abstract type can refer to an object of a subclass of that type.! This permits polymorphism.! Sometimes a collection, such as an array, of a superclass or abstract superclass type contains objects of subclasses.! An iterator is used to traverse all objects in the collection.! A message sent to each object behaves in a polymorphic manner. Inheritance & Polymorphism 6

Case Study : Point,Circle,Cylinder (1) public abstract class Shape extends Object public double area() return 0.0; public double volume() return 0.0; public abstract String getname(); public abstract void Draw(); Inheritance & Polymorphism 7

Case Study : Point,Circle,Cylinder (2) import javax.swing.joptionpane; public class Point extends Shape protected int x, y; // coordinates of the Point public Point() setpoint( 0, 0 ); public Point( int x, int y ) setpoint( x, y ); public void setpoint( int x, int y ) this.x = x; this.y = y; Inheritance & Polymorphism 8

Case Study : Point,Circle,Cylinder (3) public int getx() return x; public int gety() return y; public String tostring() return "[" + x + ", " + y + "]"; public String getname() return "Point"; public void Draw() JOptionPane.showMessageDialog(null,getName() + ": " + this); Inheritance & Polymorphism 9

Case Study : Point,Circle,Cylinder (4) import javax.swing.joptionpane; public class Circle extends Point // inherits from Point protected double radius; public Circle() // implicit call to superclass constructor setradius( 0 ); public Circle( double r, int x, int y ) super( x, y ); // call to superclass constructor setradius( r ); public void setradius( double r ) radius = ( r >= 0.0? r : 0.0 ); Inheritance & Polymorphism 10

Case Study : Point,Circle,Cylinder (5) public double getradius() return radius; public double area() return Math.PI * radius * radius; public String tostring() return "Center = " + "[" + x + ", " + y + "]" + "; Radius = " + radius; public String getname() return "Circle"; public void Draw() JOptionPane.showMessageDialog(null,getName() + ": " + this); Inheritance & Polymorphism 11

Case Study : Point,Circle,Cylinder (6) import javax.swing.joptionpane; public class Cylinder extends Circle protected double height; // height of Cylinder // no-argument constructor public Cylinder() // implicit call to superclass constructor here setheight( 0 ); // constructor public Cylinder( double h, double r, int x, int y ) super( r, x, y ); // call to superclass constructor setheight( h ); Inheritance & Polymorphism 12

Case Study : Point,Circle,Cylinder (7) // Set height of Cylinder public void setheight( double h ) height = ( h >= 0? h : 0 ); // Get height of Cylinder public double getheight() return height; // Calculate area of Cylinder (i.e., surface area) public double area() return 2 * super.area() + 2 * Math.PI * radius * height; Inheritance & Polymorphism 13

Case Study : Point,Circle,Cylinder (8) // Calculate volume of Cylinder public double volume() return super.area() * height; // Convert a Cylinder to a String public String tostring() return super.tostring() + "; Height = " + height; // Return the class name public String getname() return "Cylinder"; public void Draw() JOptionPane.showMessageDialog(null,getName() + ": " + this); Inheritance & Polymorphism 14

Case Study : Point,Circle,Cylinder (9) import java.text.decimalformat; import javax.swing.joptionpane; public class Tester public static void main( String args[] ) String output; Point point = new Point( 7, 11 ); Circle circle = new Circle( 3.5, 22, 8 ); Cylinder cylinder = new Cylinder( 10, 3.3, 10, 10 ); Shape arrayofshapes[]; arrayofshapes = new Shape[ 3 ]; Inheritance & Polymorphism 15

Case Study : Point,Circle,Cylinder (10) // aim arrayofshapes[0] at subclass Point object arrayofshapes[ 0 ] = point; // aim arrayofshapes[1] at subclass Circle object arrayofshapes[ 1 ] = circle; // aim arrayofshapes[2] at subclass Cylinder object arrayofshapes[ 2 ] = cylinder; point.draw(); circle.draw(); cylinder.draw(); DecimalFormat precision2 = new DecimalFormat( "0.00" ); Inheritance & Polymorphism 16

Case Study : Point,Circle,Cylinder (11) // Loop through arrayofshapes and print the name, // area, and volume of each object. for ( int i = 0; i < arrayofshapes.length; i++ ) arrayofshapes[ i ].Draw(); Polymorphism & Dynamic Binding output = "\narea = " + precision2.format( arrayofshapes[ i ].area() ) + "\nvolume = " + precision2.format( arrayofshapes[ i ].volume() ); JOptionPane.showMessageDialog( null, output,"demonstrating Polymorphism", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); Inheritance & Polymorphism 17

Interfaces (1)! Interfaces provide some features of multiple inheritance: n Like an abstract class, an interface defines a set of methods (and perhaps constants as well), but no implementation. n By using the implements keyword, a class can indicate that it implements that set of methods. n This makes it unnecessary for related classes to share a common superclass or to directly subclass object. n It s possible for a class to implement several interfaces. Inheritance & Polymorphism 18

Interfaces (2)! An interface is like a class with nothing but abstract methods and final, static fields. All methods and fields of an interface must be public.! However, unlike a class, an interface can be added to a class that is already a subclass of another class. Furthermore an interface can apply to members of many different classes! When you introduce a new class, you can choose to support any number of interfaces! For each interface you support you must implement member functions defined in the interface Inheritance & Polymorphism 19

Interfaces (3)! All interface members are public.! Methods are abstract.! Constants are static and final.! Generally, the keywords public, abstract, static and final are not used in an interface declaration since they will have these characteristics by default. Inheritance & Polymorphism 20

Interfaces (4)! A class can inherit from only one direct superclass, but it can implement multiple interfaces. public class MyClass extends MySuperClass implements IFace1, IFace2, IFace3,! If a class implements an interface but not all of its methods, it must be an abstract class.! Interface methods are implicitly abstract.! Interfaces are defined in their own.java file named with the interface name. Inheritance & Polymorphism 21

Interfaces (5) public interface IShape public String getname(); public void Draw(); public class Point extends Shape implements IShape //Previous implementation... public String getname() public void Draw() Inheritance & Polymorphism 22

Inheritance Principles! Common operations and fields belong to a superclass! Use inheritance to model the is-a relationship! Don t use inheritance unless ALL inherited methods make sense.! Use Polymorphism not type information Inheritance & Polymorphism 23