Inheritance & Polymorphism

Similar documents
Inheritance & Polymorphism. Object-Oriented Programming

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

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

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

Java Session. Day 2. Reference: Head First Java

Inheritance and Polymorphism

Programming Using C# QUEEN S UNIVERSITY BELFAST. Practical Week 7

Object Oriented Programming. Java-Lecture 11 Polymorphism

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Object-Oriented Programming More Inheritance

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

Object Oriented Programming Part I of II. Steve Ryder 08/22/05, Session 8351 JSR Systems (JSR)

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

What is Inheritance?

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

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Java Object Oriented Design. CSC207 Fall 2014

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

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

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Chapter 10 Classes Continued. Fundamentals of Java

Instance Members and Static Members

OVERRIDING. 7/11/2015 Budditha Hettige 82

Chapter 14 Abstract Classes and Interfaces

PROGRAMMING LANGUAGE 2

Inheritance (Outsource: )

First IS-A Relationship: Inheritance

More Relationships Between Classes

Object-Oriented ProgrammingInheritance & Polymorphism

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

Example: Count of Points

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

CS Programming I: Inheritance

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

QUESTIONS FOR AVERAGE BLOOMERS

Inheritance. Transitivity

Chapter 5 Object-Oriented Programming

Logistics. Final Exam on Friday at 3pm in CHEM 102

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

CS-202 Introduction to Object Oriented Programming

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

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

public UndergradStudent(String n, String m, String p) { programme = p; super(n, m);

CS111: PROGRAMMING LANGUAGE II

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

ITI Introduction to Computing II

Inheritance and Interfaces

Inheritance and Polymorphism

CSC207 Week 3. Larry Zhang

Programming in C# Inheritance and Polymorphism

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

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

A A B U n i v e r s i t y

ITI Introduction to Computing II

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

Inheritance and Polymorphism in Java

Polymorphism and Inheritance

Inheritance -- Introduction

BBM 102 Introduction to Programming II Spring Inheritance

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

Programming II (CS300)

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

COMP 110/L Lecture 20. Kyle Dewey

Inheritance and Polymorphism. CS180 Fall 2007

Lecture 18 CSE11 Fall 2013 Inheritance

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Inheritance (Deitel chapter 9)

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

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

Lecture 5: Inheritance

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

CS 251 Intermediate Programming Inheritance

OBJECT ORİENTATİON ENCAPSULATİON

Inheritance, Polymorphism, and Interfaces

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Practice for Chapter 11

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

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

Object Orientated Programming Details COMP360

Full file at Chapter 2 - Inheritance and Exception Handling

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

1. Write two major differences between Object-oriented programming and procedural programming?

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Polymorphism. Arizona State University 1

CS 11 java track: lecture 3

Java Magistère BFA

Inheritance, Polymorphism and the Object Memory Model

CS111: PROGRAMMING LANGUAGE II

Transcription:

Inheritance & Polymorphism Procedural vs. object oriented Designing for Inheritance Test your Design Inheritance syntax **Practical ** Polymorphism Overloading methods

Our First Example There will be shapes on a GUI, (lets assume the shapes needed for now are a circle, a square, and a triangle), when the user clicks on the shape, the shape will rotate clockwise 360 ( all the way around), and play a sound file specific to that shape.

Procedural programmer tools: Basic question: What are the things this program has to do?? Rotate && Playsound Loops: Statements: Branches

Procedural Programmer The initial design will look like this : rotate (shapenum) { // Make the shape rotate 360 playsound (shapenum) { // use shapenum to decide which sound file // to play and play it, you can use switch or if- // then statements here ( branching)

Object Oriented Programmer Basic questions: What are the things in the program? who are the key players?? Shapes The initial design will look like this: Square rotate() { //code to rotate a //square playsound() { // code to play the //Square sound file Circle rotate() { //code to rotate a //Circle playsound() { // code to play the //Circle sound file Triangle rotate() { //code to rotate a //triangle playsound() { // code to play the //Triangle sound file

One small modification We are going to add another shape, The new shape is called a cloud. The cloud shape is supposed to rotate around a point on one end. (all shapes used to rotate around the center point) Before After

Modifications applied: Procedural program: rotate (shapenum, xpt, ypt) { // if the shape is not a cloud //calculate the center point then //rotate //else // use the xpt, ypt as the //rotation point offset, then // rotate playsound (shapenum) { // use shapenum to decide which //sound file to play and play it Object oriented program: Add: int xpoint; int ypoint; Cloud rotate() { //code to rotate a //cloud using x, y playsound() { // code to play the //Cloud sound file

Choosing the final design Obviously both designs can do the job but when more modifications are required: the procedural program expands and a lot of code is effected. Testing, recompiling the whole program all over again, things that used to work, didn t. In the object oriented program, we maintained four different rotate methods!!! Cloud Square Circle Triangle int xpoint; int ypoint; rotate() { //code to rotate a //square playsound() { // code to play the //Square sound file rotate() { //code to rotate a //Circle playsound() { // code to play the //Circle sound file rotate() { //code to rotate a //triangle playsound() { // code to play the //Triangle sound file rotate() { //code to rotate a //cloud using x, y playsound() { // code to play the //Cloud sound file

Object Oriented final design: Super class (More abstract) Sub classes (more specific) Shape rotate() { //code to rotate playsound() { // code to play the //sound file Square Circle Triangle int xpoint; int ypoint; Cloud rotate() { //code to rotate a //cloud using x, y

Things you need to know Inheritance is one of the most important feature of Object Oriented programming Inheritance is :- Software reusability. creating new class by absorbing an existing class s members and embellishing them with new or modified capabilities.

Designing for Inheritance The animals kingdom example : Assumptions: We've been given a list of some of the animals that will be in the program,but not all of them. We want other programmers to be able to add new kinds of animals to the program at any time. The current list of animals:

First step: Variables : Food, Location, picture Methods: makenoise (), eat(), roam(), sleep(). Food Location Picture Animal Roam() Sleep() Wolf Dog Jaguar Lion tiger Cat rabbit

Which methods should we override??? No, but it seems that this programmer is just a beginner!!! Mom, Is this 4 real?? Are we supposed to eat plants?

Overriding Food Location Picture Animal Roam() Sleep() Wolf Dog Jaguar Lion tiger Cat rabbit

Looking for more inheritance opportunities Big cats: WAILAK YALLY ET3ADEENA!!!! maybe am a bit smaller but we belong to the same family

Design that makes sense: Food Location Picture Animal Roam() Sleep() Big cats Roam() rabbit Dogs Family Roam() Dog Jaguar Lion tiger Cat Wolf

Which method is called? Wolf w = new Wolf(); w.; w.roam(); Food Location Picture Animal Roam() Sleep() Dogs Family Roam() w.; w.sleep(); Wolf

Things you need to know Direct superclass Inherited explicitly (one level up hierarchy) Indirect superclass Inherited two or more levels up hierarchy Single inheritance Inherits from one superclass Multiple inheritance Inherits from multiple superclasses Java does not support multiple inheritance

Test your design (IS-A Test) Triangle extends shape // Triangle IS-A shape Lion extends BigCat //Lion IS-A Big Cat Oven extends Kitchen // sounds reasonable until you apply the IS-A test Oven and kitchen are related but not through inheritance, Oven and kitchen are joined by a HAS-A relationship.

Advantages of inheritance Minimize the amount of duplicate code in an application by sharing common code amongst several subclasses. A better organization of code and smaller, simpler compilation units.

Inheritance syntax Public class <subclass name> extends <superclass name> Public class person {.. Public class employee extends person { person employee

Note: Private members superclass s private members are not inherited by its subclasses. I'm sure I declared it in the super class, but its not inherited by any of its subclasses!! Public & Protected members superclass s Public & Protected members are inherited by its subclasses.

You Try Person Student

Output: Private access example: \Student.java:15: name has private access in Person {return name; ^ 1 error

Class object Every class in Java extends an existing class called Object (i.e. Every class inherits Object s methods) New class implicitly extends Object if it does not extend another class The Java compiler sets the superclass of a class to Object when the class declaration does not explicitly extend a superclass.

Construction under Inheritance Since every object inherits from the Object class, the Object() constructor is always called implicitly. However, you can call a superclass constructor explicitly using the built in super keyword, as long as it is the first statement in a constructor.

You Try

Output:

Polymorphism comes from Greek Poly Many Polymorphism Morph forms or shapes

Polymorphism Polymorphism enables us to "program in the general" rather than "program in the specific. Polymorphism enables programmers to deal in generalities and let the execution-time environment handle the specifics.!!!!????!

Cat mycat = new Cat(); Back to the ZOo Declare a reference variable Cat Create an object Cat Object Link the object and the reference Cat Cat Object

Polymorphism The important point is that the reference type && the object type are the same. Cat Cat Object But with polymorphism, the reference type and the object can be different. Animal mycat = new Cat(); Animal Cat Object

Polymorphism example Animal[] animals = new animal[5]; animals [0] = new Dog(); animals [1] = new Cat(); animals [2] = new Rabbit(); animals [3] = new Wolf(); for( int i=0 ; i<animals.length ; i++) { animals[i].eat(); animals[i].roam();

Overloading a method Method overloading is nothing more than having two methods with the same name but different argument lists. It has nothing to do with inheritance and polymorphism. An overloaded method is not the same as an overridden method.

Another Polymorphism example:

TestPolymorphism.java Output: