Polymorphism CT1513.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Java 212: Inheritance and Polymorphism. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override (not.
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.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
More about classes and objects Classes in Visual Basic.NET.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Inheritance using Java
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 10 Inheritance and Polymorphism
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
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
Polymorphism.
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
Class A { public : Int x; A()
UML Class Diagram: class Rectangle
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
Java Programming Language
Abstract Classes AKEEL AHMED.
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism, Abstract Classes & Interfaces
CSE 1030: Implementing GUI Mark Shtern.
Programming II Polymorphism A.AlOsaimi.
Java – Inheritance.
Introduction to Java Programming
Polymorphism Polymorphism - Greek for “many forms”
Java Inheritance.
Inheritance CT1513.
COP 3330 Object-oriented Programming in C++
Chapter 11 Class Inheritance
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Polymorphism CT1513

Polymorphism Can treat an object of a subclass as an object of its superclass A reference variable of a superclass type can point to an object of its subclass Person name, nameRef; PartTimeEmployee employee, employeeRef; name = new Person("John", "Blair"); employee = new PartTimeEmployee("Susan", "Johnson", 12.50, 45); nameRef = employee;

Polymorphism Late binding or dynamic binding (run-time binding): Method to be executed is determined at execution time, not compile time Polymorphism: to assign multiple meanings to the same method name Implemented using late binding

Polymorphism (continued) The reference variable name or nameRef can point to any object of the class Person or the class PartTimeEmployee These reference variables have many forms, that is, they are polymorphic reference variables They can refer to objects of their own class or to objects of the classes inherited from their class

Polymorphism and References Shape myShape = new Circle(); // allowed Shape myShape2 = new Rectangle(); // allowed Rectangle myRectangle = new Shape(); // NOT allowed

Note ! A A reference variable from a class A A ref; Can point to an object of class B as well as an object of class C ref = new A(); ref = new B(); ref = new C(); B C

100 200 300 1000

Polymorphism Can also declare a class final using the keyword final If a class is declared final, then no other class can be derived from this class

Abstract Classes A class that is declared with the reserved word abstract in its heading An abstract class can contain instance variables, constructors, finalizers, and non-abstract methods

Abstract Classes (continued) You cannot instantiate an object of an abstract class type; can only declare a reference variable of an abstract class type You can instantiate an object of a subclass of an abstract class, but only if the subclass gives the definitions of all the abstract methods of the superclass

Abstract Class Example public abstract class AbstractClassExample { protected int x; public void abstract print(); public void setX(int a) x = a; } public AbstractClassExample() x = 0;