Chapter 10: Method Overriding and method Overloading

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Reusable Classes.  Motivation: Write less code!
OOP: Inheritance By: Lamiaa Said.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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 and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Coming up: Inheritance
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
OOPM. Java Array Array is a collection of similar type of elements that have contiguous memory location. Java array is an object the contains elements.
Inheritance.
Functions + Overloading + Scope
Polymorphism in Methods
Inheritance and Polymorphism
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
03/10/14 Chapter 9 Inheritance.
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Modern Programming Tools And Techniques-I Inheritance
OOP’S Concepts in C#.Net
Overloading and Constructors
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Inheritance Basics Programming with Inheritance
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Comp 249 Programming Methodology
Java Programming Language
Computer Programming with JAVA
Chapter 11 Inheritance and Polymorphism
Overriding Methods & Class Hierarchies
Java Programming, Second Edition
Inheritance.
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Programming in C# CHAPTER 5 & 6
Inheritance and Polymorphism
Presentation transcript:

Chapter 10: Method Overriding and method Overloading

Overriding Methods A child class can override the definition of an inherited method in favor of its own The new method must have the same signature as the parent's method, but can have a different body The type of the object executing the method determines which version of the method is invoked A parent method can be invoked explicitly using the super reference

Method Overriding If subclass has a method of a superclass (same signature), that method overrides the superclass method: public class A { ... public int M (float f, String s) { bodyA } } public class B extends A { ... public int M (float f, String s) { bodyB } If we call M on an instance of B , bodyB runs In B we can access bodyA with: super.M(...) The subclass M must have same return type as superclass M

Method overriding Example

Method overriding Example with Super keyword

Method Overloading Method overloading: multiple methods ... With the same name But different signatures In the same class Constructors are often overloaded Argument lists could differ in – 1. Number of parameters. 2. Data type of parameters. 3. Sequence of Data type of parameters. Example: MyClass (int inputA, int inputB) MyClass (float inputA, float inputB)

Method overloading Example

Overloading – Different Number of parameters in argument list

Overloading – Difference in data type of arguments

Overloading – Sequence of data type of arguments

Valid/invalid cases of method overloading Case 1: int mymethod(int a, int b, float c) int mymethod(int var1, int var2, float var3) Result: Compile time error. Argument lists are exactly same. Both methods are having same number, data types and same sequence of data types in arguments.

Valid/invalid cases of method overloading Case 2: int mymethod(int a, int b) int mymethod(float var1, float var2) Result: Perfectly fine. Valid case for overloading. Here data types of arguments are different.

Valid/invalid cases of method overloading Case 3: int mymethod(int a, int b) int mymethod(int num) Result: Perfectly fine. Valid case for overloading. Here data types of arguments are different.

Valid/invalid cases of method overloading Case 4: float mymethod(int a, float b) float mymethod(float var1, int var2) Result: Perfectly fine. Valid case for overloading. Sequence of the data types are different, first method is having (int, float) and second is having (float, int).

Overloading vs. Overriding Don't confuse the concepts of overloading and overriding Overloading deals with multiple methods with the same name in the same class, but with 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