Polymorphism Method overriding Method overloading Dynamic binding 1.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 Inheritance Classes and Subclasses Or Extending a Class.
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Object Oriented Programming with Java
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Java
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Made with love, by Zachary Langley Applets The Graphics Presentation.
Web Design & Development Lecture 19. Java Graphics 2.
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Object Oriented Programming
Chapter 1 Inheritance University Of Ha’il.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
Reusable Classes.  Motivation: Write less code!
1 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
Classes, Interfaces and Packages
Welcome back!. Object Oriented Programming – Encapsulation Classes encapsulate state (fields) and behavior (methods) – Polymorphism Signature Polymorphism.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
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.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Polymorphism.
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance, Polymorphism, and Interfaces
Inheritance, Polymorphism, and Interfaces. Oh My
Software Design Lecture : 12.
Polymorphism CT1513.
Polymorphism Polymorphism - Greek for “many forms”
Chapter 11 Inheritance and Polymorphism Part 1
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Polymorphism Method overriding Method overloading Dynamic binding 1

Introduction to polymorphism Polymorphism is the ability to take more than one form Earlier on we discussed polymorphism through operator overloading where an operator like “+” can be used for math addition, string concatenation, and for matrix addition. This is equivalent to a word having different meanings depending on the context 2

Introduction to polymorphism – cont’d Polymorphism enables objects with different internal structures to share the same external interface This means same interface may be accessed by different classes that work differently as shown in figure below: 3

Fig: Polymorphism 4 Draw() Circle Draw() Rectangle Draw() Triangle Draw()

Introduction to polymorphism – cont’d As we saw earlier, Java doesn’t implement operator overloading except the “+” operator This lecture discusses how to implement polymorphism using method overriding and method overloading. 5

Method overriding Overriding means redefining a method of a class in its subclass The method name and signature (number and type of parameters it requires) remain unchanged i.e. only the method body changes 6

Example: Overriding public class Super { int x; Super(int x) { this.x=x; } void display() { System.out.println("Super x = "+x); } 7

Example: Overriding public class Sub extends Super { int y; Sub (int x, int y) { super(x); this.y = y; } void display() { System.out.println("Super x = "+ x); System.out.println("Sub y = "+ y); } 8

Example: Overriding public class OverrrideTest { public static void main(String args[]) { Sub a = new Sub(100,200); a.display(); } 9

Example: Overriding We use this keyword to access a variable of the current class We use the inbuilt super method to access a variable of the super class In the above example, the Sub class display() method overrides the Super class display() method If we call the Sub class, we get a different behavior from that of the Super class 10

Example: Overriding in Applets Applets always inherit from the Applet class This class has methods such as init(), start(), stop(), destroy These methods are available for use by applets to give them their runtime behavior 11

Example: Overriding in Applets Applets are good examples of both the inheritance and the polymorphism principles. To make an applet unique, various life cycle methods are overridden as shown in example below 12

Example: Overriding in Applets import java.awt.*; import java.applet.*; public class NestedApplet extends Applet { // display params int width=400; int height=200; int level=100; int inc=10; // first override the life cycle methods public void init() { System.out.println("Initializing"); incNesting(); } 13

Example: Overriding in Applets public void start() { System.out.println("Starting."); incNesting(); } public void stop() { System.out.println("Stopping."); incNesting(); } public void destroy() { System.out.println("Shutting down."); incNesting(); } 14

Example: Overriding in Applets public void paint(Graphics g) { int i, shift=0; g.setColor(Color.blue); for (i=0;i<level;i++) { g.drawRect(shift,shift,width-2*shift-1,height- 2*shift-1); shift = shift + inc; } g.drawString("Nesting level = "+level,width/2- 50,height/2+5); } public void incNesting() { level++; repaint(); } 15

Overloading Overloading is another technique to implement polymorphism Here the method name is retained, but the signature changes Used when a method is required to perform similar tasks but using different input parameters 16

Overloading – cont’d When we call a method, Java matches the Method name, Number of parameters, and Type of parameters The matching method is then executed 17

Overloading – cont’d Overloading does not require inheritance All we need to do is define several versions of the same method Defined methods should have different number and type of parameters Change their body definition as appropriate We can overload both constructors and ordinary methods 18

Overloading – cont’d In the example below, the constructor method is overloaded such that one takes one integer parameter and the other takes two integer parameters The matching will be done at runtime, depending on how many parameters you supply 19

Example: Overloading class Room { int length, breadth; Room(int x, int y) { length = x; breadth = y; } Room(int x) { length = breadth=x; } int area() { return(length*breadth); } 20

Example: Overloading class RectangleSquare { public static void main(String args[]) { Room room1 = new Room(25, 15); Room room2 = new Room(20); int area1 = room1.area(); int area2 = room2.area(); System.out.println("Area of Rectangle = "+area1); System.out.println("Area of Square = "+area2); } 21

Overloading vs. Overriding Don't confuse the concepts of overloading and 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 22

Dynamic Binding Binding is the Linking of a procedure call to the code to be executed in response to the call Dynamic binding is the The code to be executed is not known until the time of the call at runtime Associated with polymorphism and inheritance. 23

Example: Dynamic Binding 24 Shape Draw() Circle Object Draw(Circle) Box Object Draw(Box) Rectangle Object Draw(Rectangle)

Dynamic binding example By inheritance, Each of the object will invoke the draw procedure The algorithm will however be known at runtime depending on whether the user wants to draw circle or box or rectangle Thus, at runtime The code matching the object under current reference will be called. 25