Inheritance and Polymorphism

Similar documents
Object Oriented Programming. Java-Lecture 11 Polymorphism

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

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

CS-202 Introduction to Object Oriented Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

BBM 102 Introduction to Programming II Spring Inheritance

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

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

Programming II (CS300)

Inheritance -- Introduction

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Object Oriented Design. CSC207 Fall 2014

PROGRAMMING LANGUAGE 2

OVERRIDING. 7/11/2015 Budditha Hettige 82

Lecture 18 CSE11 Fall 2013 Inheritance

Polymorphism and Inheritance

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

CS1150 Principles of Computer Science Objects and Classes

8. Polymorphism and Inheritance

Inheritance. Transitivity

Chapter 5 Object-Oriented Programming

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

ITI Introduction to Computing II

Inheritance, Polymorphism, and Interfaces

Chapter 10 Classes Continued. Fundamentals of Java

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

Practice for Chapter 11

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

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

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

Full file at Chapter 2 - Inheritance and Exception Handling

CS111: PROGRAMMING LANGUAGE II

COMP 110/L Lecture 19. Kyle Dewey

ITI Introduction to Computing II

Programming II (CS300)

Inheritance and Interfaces

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.

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

CLASSES AND OBJECTS IN JAVA

Inheritance and Interfaces

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

CS111: PROGRAMMING LANGUAGE II

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

Making New instances of Classes

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

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

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

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

What is Inheritance?

Chapter 14 Abstract Classes and Interfaces

ECE 122. Engineering Problem Solving with Java

Classes and Inheritance Extending Classes, Chapter 5.2

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

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

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

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

Inheritance (continued) Inheritance

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

Inheritance and Polymorphism

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

Inheritance. OOP: Inheritance 1

Inheritance and Encapsulation. Amit Gupta

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

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

CMSC 132: Object-Oriented Programming II

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

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

Instance Members and Static Members

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

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

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

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Computer Programming Inheritance 10 th Lecture

CS111: PROGRAMMING LANGUAGE II

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

Inheritance and object compatibility

Lecture 7: Classes and Objects CS2301

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

CMSC 132: Object-Oriented Programming II

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Inheritance Introduction. 9.1 Introduction 361

CS111: PROGRAMMING LANGUAGE II

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Create a Java project named week9

Inheritance & Polymorphism

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

COMP 110/L Lecture 20. Kyle Dewey

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

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

C++ Important Questions with Answers

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

Inheritance CSC9Y4. Inheritance

Object Oriented Programming: Based on slides from Skrien Chapter 2

Object Oriented Relationships

Chapter 7. Inheritance

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

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

Transcription:

Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg eaymanelshenawy@yahoo.com eaymanelshenawy@gmail.com Lecture 4 Inheritance and Polymorphism 1

Objectives IN THIS Lecture, YOU WILL: Learn about inheritance Learn about subclasses and superclasses Explore how to override the methods of a superclass Examine how constructors of superclasses and subclasses work Learn about polymorphism Examine abstract classes Become familiar with interfaces Learn about composition 4 Encapsulation: OOP you can combine data and operations on that data in a single unit Operations can (directly) access the data, but the internal state of an object cannot be manipulated directly. 5 2

Inheritance You can create new classes from an existing classes. subclass (Circle) can extend base class (Shape). subclass (Car) can extend base class (vehicle). The new created class is called a subclass /derived class; The original class is called super or base class Reuse the code and save the programmer time. Inheritance ( is-a relationship) Composition (aggregation) ( has-a relationship) Many common features Best class design to avoid redundancy? 6 Inheritance The inheritance hierarchy describes a relationship between entities. A poodle is a dog, which is a mammal, which is an animal. A poodle has the same properties as a dog and can do anything a dog can do PartTimeEmployee (pay rate, and number of hours worked) is a person (first name, and last name) Able to extend the definition of the class Person by adding additional members (data and/or methods). 7 3

public class Person { private String firstname; //store the first name private String lastname; //store the last name //Default constructor; //Initialize firstname and lastname to empty string. public Person() { firstname = ""; lastname = ""; } //Set firstname and lastname public Person(String first, String last){ setname(first, last); } //Method to output the first name and last name public String tostring(){ return (firstname + " " + lastname); } //Method to set firstname and lastname public void setname(string first, String last) { firstname = first; lastname = last; } //Method to return the firstname public String getfirstname() { return firstname; } Inheritance } //Method to return the lastname public String getlastname(){ return lastname; } 8 Inheritance Shape is the superclass. The classes Circle and Rectangle are derived classes from Shape, and the class Square is derived class from Rectangle. Every Circle and every Rectangle is a Shape. Every Square is a Rectangle. 9 4

Super class and sub class rules 1. The private members of the superclass are private to the superclass; hence, the members of the subclass(es) cannot access them directly. 2. The subclass can directly access the public members of the superclass. 3. The subclass can include additional data and/or method members. 4. The subclass can override (redefine) the public methods of the superclass. The same method, number, and types of may be exist in the sub-class. Only affect objects of the subclass, not to the objects of the superclass. 5. All data members/methods of the superclass are also data members of the subclass (unless overridden). 10 single inheritance and multiple inheritance Single inheritance: the subclass is derived from a single superclass; Multiple inheritance, the subclass is derived from more than one superclass. Java supports only single inheritance; that is, in Java a class can extend the definition of only one class. 11 5

single inheritance and multiple inheritance Single inheritance: the subclass is derived from a single superclass; Multiple inheritance, the subclass is derived from more than one superclass. Java supports only single inheritance; that is, in Java a class can extend the definition of only one class. 12 Overriding A class SubClass is derived from a class SuperClass. The SubClass data and methods are the SuperClass data and methods + its own data and methods. Overriding : adding a method with the same signature as given by the superclass. To override a public method of the superclass in the subclass, the corresponding method in the subclass must have the same name, type, and parameter list. Overloading: If the corresponding method in the superclass and the subclass has the same name but different parameter lists, then this is method overloading in the subclass, which is also allowed. Whether you override or overload a method of the superclass in the subclass, you must know call to the superclass that has the same name as that used by a method of the subclass. 13 6

Rectangle Example UML class diagram of the class Rectangle. Rectangle Example 7

Class Box Example The class Box overrides the methods tostring and area, and overloads the method setdimension. Class Box Example 8

Keyword: super How to access superclass? keyword super refers to the superclass super can be used: To call a superclass constructor To call a superclass method To call an override method use Super.methodName(parms) To call non-override method Use the name of the method and parameters. 18 Keyword: super 19 9

Keyword: super 20 Are superclass s Constructors Inherited? No. They are not inherited. How to invoke superclass s constructors? Explicitly using the super keyword. Implicitly call the no-arg constructor if it exists. (Otherwise: compilation error) 21 10

Superclass s Constructor Is Always Invoked Subclass s constructor may invoke any of superclass s constructors. If none invoked explicitly, super() inserted at beginning. public A() { } is equivalent to public A() { super(); } public A(double d) { // some statements } is equivalent to public A(double d) { super(); // some statements } 22 Keyword: super (explicitly) specify a call to the default constructor of the class Rectangle, Use the reserved word super with no parameters, Call to the (default) constructor of the superclass must be the first statement. When this constructor of Box executes, it triggers the execution of the constructor with two parameters of type double of the class Rectangle. 23 11

How Super class and sub-class works 24 25 12

26 CAUTION You must use the keyword super to call the superclass constructor. Syntax error: Invoking superclass s constructor by name in subclass. Call to a superclass constructor must appear first in constructor. 27 13

public class Person { private String firstname; //store the first name private String lastname; //store the last name //Default constructor; //Initialize firstname and lastname to empty string. public Person() { firstname = ""; lastname = ""; } //Set firstname and lastname public Person(String first, String last){ setname(first, last); } //Method to output the first name and last name public String tostring(){ return (firstname + " " + lastname); } //Method to set firstname and lastname public void setname(string first, String last) { firstname = first; lastname = last; } //Method to return the firstname public String getfirstname() { return firstname; } Inheritance } //Method to return the lastname public String getlastname(){ return lastname; } 28 Inheritance 29 14

PartTimeEmployee Constructors 30 31 15

Protected Members of class if a member of a superclass needs to be accessed directly (only) by a subclass, that member is declared using the modifier protected. 32 33 16