Polymorphism in Methods

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

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.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes 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.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
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.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance and Access Control CS 162 (Summer 2009)
Sadegh Aliakbary Sharif University of Technology Spring 2011.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
Polymorphism Lecture - 9.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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 Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Sections Inheritance and Abstract Classes
Advanced Programming in Java
Inheritance and Polymorphism
ATS Application Programming: Java Programming
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Abstract Classes AKEEL AHMED.
Advanced Java Topics Chapter 9
Java – Inheritance.
Polymorphism CT1513.
Polymorphism Polymorphism - Greek for “many forms”
Java Inheritance.
Chapter 10: Method Overriding and method Overloading
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Method Overriding and method Overloading
Advanced Programming in Java
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Inheritance and Polymorphism
Presentation transcript:

Polymorphism in Methods There are two types of polymorphism in methods: Overloading and Overriding Overloading In any class, you can use the same name for different methods with different signature. Signature = name + list of parameter types Overloading is resolved by the compiler at compile time. The compiler will choose the correct method by matching on argument types

Polymorphism in Methods Overriding A method in a subclass can have the same name and signature as a method in the superclass. Overriding is resolved at runtime. if an object of the subclass, the subclass method is invoked if an object of the superclass, the superclass method is invoked technical term: late binding or delayed binding.

Forcing Overriding off: Final A class which has the keyword final at the start of a class declaration cannot be extended! A method with the keyword final at the head of its declaration cannot be overridden when its class is inherited!

Forcing Overriding: Abstract The keyword abstract forces overriding to take place. When used at the start of a class definition: zero or more of the class methods are abstract An abstract method has no body labelling a method abstract forces some subclass to override it and provide a concrete implementation for the method. Labelling a method abstract requires the class to be labelled abstract as well.

Superclass/Subclass Compatibility superclass = subclass // always valid subclass = (subclass) superclass // valid at compile time, checked at runtime subclass = superclass // not valid, requires a cast to compile someClass = unrelatedClass // won’t compile someClass = (someClass) unrelatedClass // won’t compile Java language is static/manifestly typed, supports partial type inference and is strongly typed.

Static vs Dynamic typing In static typing all expressions have their types determined prior to the program being run at compile-time. For example, 1 and (2+2) are integer expressions they cannot be passed to a function that expects a string. Statically-typed languages can be manifestly typed or type-inferred. Most statically-typed languages, such as C++ and Java, are manifestly typed. However, many manifestly typed languages support partial type inference; As seen above Java infer types in certain limited cases.

Weak and strong typing Weak typing allows a value of one type to be treated as another, for example treating a string as a number. This can occasionally be useful, but it can also cause bugs; such languages are often termed unsafe. Strongly-typed languages are often termed type-safe or safe, but they do not make bugs impossible. Java is strongly typed. As show above casting can be used to treat a child as a parent Want to study more: http://www.sitepoint.com/typing-versus-dynamic-typing/

Actual Classes, Abstract Classes and Interfaces