Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Chapter 1 Inheritance University Of Ha’il.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Coming up: Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Inheritance and Polymorphism
Object-Oriented Programming
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
ATS Application Programming: Java Programming
Object Oriented Programming
Designing for Inheritance
Modern Programming Tools And Techniques-I Inheritance
Inheritance Basics Programming with Inheritance
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Inheritance, Polymorphism, and Interfaces. Oh My
Computer Programming with JAVA
Inheritance Inheritance is a fundamental Object Oriented concept
Overriding Methods & Class Hierarchies
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 10: Method Overriding and method Overloading
Inheritance.
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Inheritance and Polymorphism

Inheritance One of the fundamental object-oriented design techniques. Advantages: Code sharing. To minimize duplication. To maintain update consistency.

Example Person Name SSN Sex Date of birth Marital status if married or widowed a. Date of marriage b. Number of children if divorced a. Date of divorce b. First divorce if single a. Independent residency

Use only one class Person Name SSN Sex Date of birth Marital status Date of marriage Number of children Date of divorce First divorce Independent residency Some operations Waste memory space

Use 3 independent classes Married Name SSN Sex Date of birth Marital status Date of marriage Number of children Some operations Divorced Name SSN Sex Date of birth Marital status Date of divorce First divorce Some operations Single Name SSN Sex Date of birth Marital status Independent residency Some operations Can’t share methods Any change must be done 3 times

Use inheritance Person Name SSN Sex Date of birth Marital status Some operations Married Date of marriage Number of children Divorced Date of divorce First divorce Single Independent residency

Deriving Classes class classname extends parent-class { [variable declaration;] [method declaration;] } Deriving Classes Superclass (Base/parent class) subclass extends superclass A subclass is A superclass Subclass (Derived class) Inheritance is used to model the Is-A relationship. Inheritance - inherits fields and methods of its superclass. Advantage of inheritance: Code reuse

Overriding Methods A child class can override the definition of an inherited method in favor of its own That is, a child can redefine a method that it inherits from its parent The new method must have the same signature as the parent's method, but can have different code in the body The type of the object executing the method determines which version of the method is invoked

Method Overriding class A { // Base class int method1() { …} float method2(int x) { …} } class B extends A { // subclass float method2(int z) { …} void method3() { …}

A method overriding error class A { // Base class int method1() { …} float method2(int x) { …} } class B extends A { // subclass char method2(int z) { …} void method3() { …} If the argument types match, the return type must match also

Overloading vs. Overriding Overloading deals with multiple methods in the same class with the same name but different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature Overloading lets you define a similar operation in different ways for different data Overriding lets you define a similar operation in different ways for different object types

The super and this references super: reserved word used in a derived class to refer to its parent class allows us to access those members of the parent class that are not inherited example: invoking the parent’s constructor class A { // Base class f(int x) { …} } class B extends A { // subclass f(int z) { …} … super.f(); this.f(); // same as f();

Polymorphism Literally polymorphism is: multiple forms. In programming, polymorphism means: one interface, multiple implementations of a method (overriding). Which implementation of the method actual called depends on the type of the object passed to the method. Advantage: Can write generic code that can be applied to different kinds of objects. In Java, polymorphism is implemented using a technique called dynamic (or late) method binding: which exact method to call is determined at run time.

Basis of Polymorphism Inheritance Method overriding Polymorphic assignment Polymorphic methods In Java, all methods are polymorphic. That is, choice of method depends on the object.

Polymorphic assignment SuperClassVariable = SubclassObject;

Example: Polymorphism Furniture void prnt(); abstract class Furniture { public int numlegs; } abstract class Chair extends Furniture { public String fabric; abstract void prnt(); class Recliner extends Chair { void prnt() { System.out.println(“I’m a recliner”); class LaZBoy extends Recliner { System.out.println(“I’m a lazboy”); Chair void prnt(); Recliner void prnt(); LaZBoy void prnt(); What is the output? Chair cha; cha = new LaZBoy(); cha.prnt(); Furniture furn; furn = new Recliner(); furn.prnt(); Furniture furn; furn = new LaZBoy(); furn.prnt();

Example: Polymorphism Furniture void prnt(); void display(Furniture furn) { furn.prnt(); } Chair void prnt(); What is the output? display(new LaZBoy()); Recliner void prnt(); display(new Recliner()); LaZBoy void prnt();

Example: Polymorphism class Queue { Object e; public String toString() { String Str = “”; for every element e in the queue Str += e.toString() + “ “; return Str; } Object String toString(); Token Operator String toString(); Operand String toString();

Members use compile-time binding class Base{ int X=99; public void prnt(){ System.out.println("Base"); } class Rtype extends Base{ int X=-1; System.out.println("Rtype"); What is the output? Base b=new Rtype(); System.out.println(b.X); b.prnt();

Homework 1 Show the output of the following program. abstract class Furniture { abstract void prnt();} class Recliner extends Furniture { void prnt() { System.out.println("I'm a recliner");} } class LaZBoy extends Recliner { void prnt() { System.out.println("I'm a lazboy");} public class furnitureTest2 { public static void main(String[] args) { Furniture [] A = { new Recliner(), new Recliner(), new LaZBoy()}; for (int i=0; i<3; ++i) A[i].prnt(); Homework 1

Homework 2 Show the output of the following program. class A { int x = 1; } class B extends A { } class C extends B { int x = 2;} public class classTest { public static void main(String[] args) { A w = new A(); System.out.println(w.x); B u = new B(); System.out.println(u.x); C v = new C(); System.out.println(v.x); A [] a = { new A(), new B(), new C()}; for (int i=0; i<3; ++i) System.out.println(a[i].x); } Homework 2