Sadegh Aliakbary Sharif University of Technology Spring 2011.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
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.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
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.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 12: Adding Functionality to Your Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance in the Java programming language J. W. Rider.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 10 Inheritance and Polymorphism
Peyman Dodangeh Sharif University of Technology Fall 2014.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSCI-383 Object-Oriented Programming & Design Lecture 17.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Design issues for Object-Oriented Languages
Polymorphism in Methods
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Polymorphism.
Advanced Programming in Java
Inheritance and Polymorphism
More inheritance, Abstract Classes and Interfaces
Advanced Programming in Java
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Java Inheritance.
Polymorphism.
Advanced Programming in Java
Computer Science II for Majors
Presentation transcript:

Sadegh Aliakbary Sharif University of Technology Spring 2011

Agenda Polymorphism Final Methods Spring 2011Sharif University of Technology2

Polymorphism Suppose Child is a subclass of Parent class. Remember : A Child’s object is also a Parent’s object is-a relationship So these lines are valid: Child c = new Child(); Parent p = new Parent(); p = c; But this line is invalid: c = p; Spring 2011Sharif University of Technology4

UpCasting & DownCasting Upcasting Shape s = new Rectangle(); Circle c = new Circle(); Shape s = c; Upcasting is always valid Downcasting Circle c = s; Circle c = (Circle) s; Needs type cast May cause errors Spring 2011Sharif University of Technology5

What About Method Calls? Shape s = new Rectangle(); s.draw(); double d = s.getArea(); Circle c = new Circle(); Shape s = c; s.draw(); double d = s.getArea(); Spring 2011Sharif University of Technology6

Spring 2011Sharif University of Technology7

Compile-time Method Binding Also known as Static Binding When a method is called, compiler knows which method is called The translation is done in compile-time Spring 2011Sharif University of Technology8

Run-time Method Binding Also known as Dynamic Binding When you call a method on a subclass object Actual method is bound in runtime (If it is overridden) Performance overload Spring 2011Sharif University of Technology9

Virtual Methods In some languages (like C++) you can specify the binding mechanism for methods If a method is declared as virtual, dynamic binding is used for this method Spring 2011Sharif University of Technology10

Applications of Polymorphism Polymorphic behavior Suppose you have so many objects in a GUI application All of them have draw() operation You simply call draw() on every object It knows how to draw itself Classes : Drawable(superclass), Player, Referee, Ball, … Spring 2011Sharif University of Technology11

No Polymorphism Spring 2011Sharif University of Technology12

With Polymorphism Spring 2011Sharif University of Technology13

Hint on Array Initialization Spring 2011Sharif University of Technology14

Animal Example Spring 2011Sharif University of Technology15

Cat & Dog Spring 2011Sharif University of Technology16

Polymorphic Animals! Spring 2011Sharif University of Technology17

Final Methods You can not override final methods final keyword Static method binding for final methods Private methods are implicitly final Static methods are implicitly final Static methods are statically bound Variable is not important No polymorphism for static variables Spring 2011Sharif University of Technology19

Final Variables You can define variables as final The value of final variable will remain constant You can not change the value of final variables You should immediately assign a value to final variables Final parameter Final local variable Final property Final static variable Spring 2011Sharif University of Technology20

Final Variables Spring 2011Sharif University of Technology21

Final Classes You can not inherit from final classes No class can extend final classes Spring 2011Sharif University of Technology22

Review of final Keyword Final data Const Local variables Method parameters Member variables Primitives  constant values Objects  constant references A compile-time constant that won’t ever change A value initialized at run time that you don’t want changed Spring 2011Sharif University of Technology23

Review of final Keyword (2) Final Methods No override Final Class No sub-class final keyword on data Different from final classes & methods Spring 2011Sharif University of Technology24

Finalism and Performance Final methods can be invoked inline Compiler can bind final methods statically Static binding So it may bring a better performance… It is now discouraged to use final to try to help the optimizer Especially with Java 6 Don’t worry about performance Java optimizer Spring 2011Sharif University of Technology25

Quiz! Write a java class for representing … Spring 2011Sharif University of Technology26

Spring 2011Sharif University of Technology27

Spring 2011Sharif University of Technology28

Go Back Spring 2011Sharif University of Technology29

Spring 2011Sharif University of Technology30