28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.

Slides:



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

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
15-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
LECTURE 07 Programming using C# Inheritance
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
What is inheritance? It is the ability to create a new class from an existing class.
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CSC 142 Computer Science II Zhen Jiang West Chester University
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Inheritance The Basics in Java. Definition  A class that is derived from another class is called a subclass (also a derived class, extended class, or.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Polymorphism 11-Nov-18.
Polymorphism 11-Nov-18.
Polymorphism 15-Nov-18.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Polymorphism 28-Nov-18.
Inherited Classes in Java
METHOD OVERRIDING in JAVA
Java Inheritance.
Object Oriented Programming in JAVA
CMSC 202 Generics.
Chapter 14 Abstract Classes and Interfaces
Polymorphism 15-Apr-19.
Polymorphism 21-Apr-19.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Programming in C# CHAPTER 5 & 6
Presentation transcript:

28-Dec-04polymorhism.ppt1 Polymorphism

28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function or method from another in C, every function has to have a different name in Java, two methods have to differ in their names, or in the number or types of their parameters foo(int i) and foo(int i, int j) are different foo(int i) and foo(int k) are the same foo(int i, double d) and foo(double d, int i) are different in C++, the signature also includes the return type but not in Java!

28-Dec-04polymorhism.ppt3 poly + morphism poly is Latin for “many”; morph is Latin for “form” in Java, a method or constructor may have multiple signatures this is called overloading EXAMPLE: Cat c1 = new Cat(); Cat c2 = new Cat(“Felix”); // overloaded constructor c1.speak(); c1.speak(3); // overloaded method overloading permits multiple ways to construct an object, or multiple ways to call a method

28-Dec-04polymorhism.ppt4 polymorphism in inheritance a method or constructor may be modified in a subclass during inheritance this is called overriding EXAMPLE: // assuming both Cat and Dog extend Animal, and Animal has // a speak() method defined: Cat c = new Cat(); c.speak(); // overridden to speak specifically like a Cat Dog d = new Dog(); d.speak(); // overridden to speak specifically like a Dog

28-Dec-04polymorhism.ppt5 an example of overloading class Test { public static void main(String args[]) { myPrint(5); myPrint(5.0); } static void myPrint(int i) { System.out.println("int i = " + i); } static void myPrint(double d) { // same name, different parameters System.out.println("double d = " + d); } } int i = 5 double d = 5.0

28-Dec-04polymorhism.ppt6 Java uses the most specific method class Test { public static void main(String args[]) { myPrint(5); myPrint(5.0); } static void myPrint(double d) { System.out.println("double: " + d); } static void myPrint(int i) { System.out.println("int: " + i); } } int:5 double: 5.0

28-Dec-04polymorhism.ppt7 multiple constructors I You can “overload” constructors as well as methods: Counter() { count = 0; } Counter(int start) { count = start; }

28-Dec-04polymorhism.ppt8 multiple constructors II one constructor can “call” another constructor in the same class, but there are special rules you call the other constructor with the keyword this the call must be the very first thing the constructor does Point(int x, int y) { this.x = x; this.y = y; sum = x + y; } Point() { this(0, 0); } a common reason for overloading constructors is (as above) to provide default values for missing parameters

28-Dec-04polymorhism.ppt9 superclass construction I the very first thing any constructor does, automatically, is call the default constructor for its superclass class Foo extends Bar { Foo() { // constructor super(); // invisible call to superclass constructor... you can replace this with a call to a specific superclass constructor use the keyword super this must be the very first thing the constructor does class Foo extends Bar { Foo(String name) { // constructor super(name, 5); // explicit call to superclass constructor...

28-Dec-04polymorhism.ppt10 superclass construction II Unless you specify otherwise, every constructor calls the default constructor for its superclass class Foo extends Bar { Foo() { // constructor super(); // invisible call to superclass constructor... You can use this(...) to call another constructor in the same class: class Foo extends Bar { Foo(String message) { // constructor this(message, 0, 0); // your explicit call to another constructor... You can use super(...) to call a specific superclass constructor class Foo extends Bar { Foo(String name) { // constructor super(name, 5); // your explicit call to some superclass constructor... Since the call to another constructor must be the very first thing you do in the constructor, you can only do one of the above

28-Dec-04polymorhism.ppt11 how to override a method create a method in a subclass having the same signature as a method in a superclass that is, create a method in a subclass having the same name and the same number and types of parameters parameter names don’t matter, just their types restrictions: the return type must be the same the overriding method cannot be more private than the method it overrides

28-Dec-04polymorhism.ppt12 calling an overridden method when your class overrides an inherited method, it basically “hides” the inherited method within this class (but not any other), you can still call the overridden method, by prefixing the call with super. example: super.printEverything();

28-Dec-04polymorhism.ppt13 the end