Advanced Programming in Java

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.
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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 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.
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.
Inheritance, Polymorphism, and Virtual Functions
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.
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.
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 Spring 2011.
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.
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
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.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Design issues for Object-Oriented Languages
Polymorphism in Methods
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Advanced Programming in Java
Polymorphism.
Advanced Programming in Java
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Methods Attributes Method Modifiers ‘static’
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
More inheritance, Abstract Classes and Interfaces
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance, Polymorphism, and Virtual Functions
Chapter 9 Inheritance and Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism Polymorphism - Greek for “many forms”
Java Inheritance.
Advanced Programming in Java
Object Oriented Programming
Polymorphism.
Lecture 18: Polymorphism (Part II)
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 2
Computer Science II for Majors
Presentation transcript:

Advanced Programming in Java Sadegh Aliakbary Sharif University of Technology Fall 2011

Agenda Polymorphism Final Methods Fall 2011 Sharif University of Technology

Polymorphism

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; Fall 2011 Sharif University of Technology

UpCasting & DownCasting 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 Fall 2011 Sharif University of Technology

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

Fall 2011 Sharif University of Technology

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 Fall 2011 Sharif University of Technology

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 Fall 2011 Sharif University of Technology

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 Fall 2011 Sharif University of Technology

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, … Fall 2011 Sharif University of Technology

No Polymorphism Fall 2011 Sharif University of Technology

With Polymorphism Fall 2011 Sharif University of Technology

Hint on Array Initialization Fall 2011 Sharif University of Technology

Animal Example Fall 2011 Sharif University of Technology

Cat & Dog Fall 2011 Sharif University of Technology

Polymorphic Animals! Fall 2011 Sharif University of Technology

More on Polymorphism Later! Fall 2011 Sharif University of Technology

Final

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 Fall 2011 Sharif University of Technology

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 Fall 2011 Sharif University of Technology

Final Variables Fall 2011 Sharif University of Technology

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

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 Fall 2011 Sharif University of Technology

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

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 Fall 2011 Sharif University of Technology

Quiz! Write a java class for representing … Fall 2011 Sharif University of Technology

More on Polymorphism

class Parent{ public void f(){ System. out class Parent{ public void f(){ System.out.println("f() in Parent"); } class Child extends Parent{ 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)"); Fall 2011 Sharif University of Technology

What is the output of: Child child = new Child(); Parent parent = new Parent(); Parent parentRefToChild = new Child(); parent.f(); child.f(); parentRefToChild.f(); Output: f() in Parent f() in Child Fall 2011 Sharif University of Technology

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 Output: method(Parent) method(Child) Fall 2011 Sharif University of Technology

Note: Overloading method() is overloaded in SomeClass 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 Fall 2011 Sharif University of Technology

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 Fall 2011 Sharif University of Technology

What is the output of: SomeSubClass ref = new SomeSubClass(); ref.method(parent); ref.method(child); ref.method(parentRefToChild); Output: method(Parent) method(Child) Fall 2011 Sharif University of Technology

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?! Fall 2011 Sharif University of Technology

Fall 2011 Sharif University of Technology