Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
(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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
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
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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 Spring 2011.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Object Oriented Programming
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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Lecture 12 Implementation Issues with Polymorphism.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
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.
Advanced Programming in Java
Inheritance and Polymorphism
More inheritance, Abstract Classes and Interfaces
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming in Java
Polymorphism Polymorphism - Greek for “many forms”
Java Inheritance.
Inheritance and Polymorphism
Advanced Programming in Java
Presentation transcript:

Sadegh Aliakbary

Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to 2JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Agenda Polymorphism Final Methods 3JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Animals can talk! 5JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Talk request You can send a talk request to an animal What does it do? It depends on type of the animal 6JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Musical Instruments The same note Different sounds on different instruments 7JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Polymorphic Behavior Common interface The same request Different behaviors Depending on the type of object 8JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Polymorphism The same interface animal.talk() instrument.play(int note) But different implementation in different classes 9JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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; 10JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

UpCasting Upcasting Shape s = new Rectangle(); Circle c = new Circle(); Shape s = c; Upcasting is always valid 11JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

DownCasting Downcasting Shape s = … Circle c = s; Circle c = (Circle) s; Needs type cast May cause errors 12JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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(); 13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

14JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 15JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Run-time Method Binding Also known as Dynamic Binding When you call a method on a superclass reference Actual method is bound in runtime (If it is overridden) Performance overload 16JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 that method 17JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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, … 18JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

No Polymorphism 19JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

With Polymorphism 20JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Hint on Array Initialization 21JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Animal Example 22JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Cat & Dog 23JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Polymorphic Animals! 24JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

More on Polymorphism Later! 25JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 Invoked reference is not important No polymorphism for static variables 27JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 28JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Final Variables 29JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Final Classes You can not inherit from final classes No class can extend final classes 30JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 31JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Review of final Keyword (2) Final Methods No override Final Class No sub-class final keyword on data Different from final classes & methods 32JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 33JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

class Parent{ public void f(){ System.out.println("f() in Parent"); } class Child extends Parent{ public void f(){ System.out.println("f() in Child"); } public class SomeClass { public void method(Parent p){ System.out.println("method(Parent)"); } public void method(Child p){ System.out.println("method(Child)"); } 35JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

What is the output of: Child child = new Child(); Parent parent = new Parent(); Parent parentRefToChild = new Child(); parent.f(); child.f(); parentRefToChild.f(); 36 Output: f() in Parent f() in Child JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

What is the output of: SomeClass square = new SomeClass(); square.method(parent); square.method(child); square.method(parentRefToChild); Important Note: Polymorphic behavior for reference the reference before dot Not for the parameters 37 Output: method(Parent) method(Child) method(Parent) JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Note: Overloading public class SomeClass { public void method(Parent p){ System.out.println("method(Parent)"); } public void method(Child p){ System.out.println("method(Child)"); } method() is overloaded in SomeClass Two independent methods 38JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

To Overload or to Override class SomeClass { public void method(Parent p){ System.out.println("method(Parent)"); } class SomeSubClass extends SomeClass{ public void method(Child p){ System.out.println("method(Child)"); } method() is overloaded in SomeSubClass It is not overridden Two independent methods 39JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

What is the output of: SomeSubClass ref = new SomeSubClass(); ref.method(parent); ref.method(child); ref.method(parentRefToChild); Output: method(Parent) method(Child) method(Parent) 40JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

A Question When we override equals() method Why do we pass Object as the parameter? For example class Person has an equals method like this: public boolean equals(Object obj) {…} But not like this: public boolean equals(Person obj) {…} Why?! 41JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

42JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source