0% found this document useful (0 votes)
0 views21 pages

oop lec 07

The document discusses method overloading and method overriding in Java, explaining compile-time and run-time polymorphism. It covers the concepts, rules, syntax, and provides examples for both method overloading and method overriding. Additionally, it addresses common questions regarding static methods and their relation to overriding.

Uploaded by

sjadhfwefqw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views21 pages

oop lec 07

The document discusses method overloading and method overriding in Java, explaining compile-time and run-time polymorphism. It covers the concepts, rules, syntax, and provides examples for both method overloading and method overriding. Additionally, it addresses common questions regarding static methods and their relation to overriding.

Uploaded by

sjadhfwefqw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Method overloading and

Method overriding
JAVA

Instructor Name: Syed Shahzad Hassan


Department of Computer Science, HITEC University Taxila - Pakistan
Contents
2

 Recap of previous lecture


 Quiz solution discussion
 Compile-time Polymorphism / Method overloading
 Run-time Polymorphism / Method overriding
Recap
3

 What is inheritance, uses, terms and its types


Compile-time Polymorphism (Concept)
4

• Compile-time polymorphism, often referred to as static


polymorphism or method overloading, is one of the fundamental
aspects of Java development.

• Multiple methods having the same name but different


parameters.

• can be with a different number of arguments or different data


types of arguments

• The appropriate method is chosen by the compiler based on the


parameters provided at compile time.
Compile-time Polymorphism (Program)

5
Compile-time Polymorphism (Program)

6
Compile-time Polymorphism (Program using
scanner class)

7
Run-time Polymorphism: Method Overriding (Concept)
8

If subclass (child class) has the same method as declared in the


parent class, it is known as method overriding in Java.

In other words, If a subclass provides the specific


implementation of a (general) method that has been declared
by its parent class, it is known as method overriding.
In Java, we can override methods only, not the variables(data members), so runtime polymorphism
cannot be achieved by data members.
Run-time Polymorphism: Method Overriding (Concept)
9

• Method overriding is used to provide the specific


implementation of a method which is already provided
by its superclass.

• Method overriding is used for runtime polymorphism.


Rules for Java Method Overriding
10

• The method must have the same name as in the


parent class

• The method must have the same parameter as in the


parent class.

• There must be inheritance.


Method Overriding (Syntax)
11

class Vehicle
{
void run()
{}
}
class Bike2 extends Vehicle
{
void run()
{}
}
Method Overriding (Example)
12

//Java Program to illustrate the use of Java Method


Overriding
//Creating a parent class.
class Vehicle
{ public static void main(String args[])
//defining a method
{
void run()
{System.out.println(“Any type of Vehicle is running");}
Bike2 obj = new Bike2();//creating object
} obj.run();//calling method
//Creating a child class }
class Bike2 extends Vehicle
{
//defining the same method as in the parent class
void run()
{System.out.println("Bike is running safely");}
}
Method Overriding (Example)
13

class Human
{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
public static void main( String args[])
}
class Boy extends Human
{
{ Boy obj = new Boy();
//Overriding method //This will call the child class version of eat()
public void eat(){ obj.eat();
System.out.println("Boy is eating"); }
}
Task
14

 Write Java code to create an Employee class with a method


introduce() that prints "I am an employee." Create a Teacher class
that extends Employee and overrides the introduce() method to print
"I am a teacher." Test your code by creating instances of both classes
and calling the introduce() method through them.
Task
15

 Write a Java program to create a class known as


"BankAccount" with methods called deposit() and withdraw().
Create a subclass called SavingsAccount that overrides the
withdraw() method to prevent withdrawals if the account
balance falls below one hundred.
Method Overriding (Example)
16
class Bank
{
int getRateOfInterest() {return 0;}
}
//Creating child classes.
class HBL extends Bank
{
int getRateOfInterest() {return 8;}
}
class UBL extends Bank
{
int getRateOfInterest() {return 7;}
}
class ABL extends Bank
{
int getRateOfInterest() {return 9;}
}
Method Overriding (Example)
17

class Test2
{
public static void main(String args[])
{
HBL h=new HBL();
UBL u=new UBL();
ABL a=new ABL();
System.out.println(“HBL Rate of Interest: "+h.getRateOfInterest());
System.out.println(“UBL Rate of Interest: "+u.getRateOfInterest());
System.out.println(“ABL Rate of Interest: "+a.getRateOfInterest());
}
}

Java method overriding is mostly used in Runtime Polymorphism which


we will learn in next lecture.
Method Overriding
18

Can we override static method?

Why can we not override static method?

Can we override java main method?


Method Overriding
19

Can we override a static method?


No, a static method cannot be overridden.

Why can not we override a static method?

It is because the static method is bound with class whereas the instance
method is bound with an object. Also, static methods are resolved at compile-
time based on the class in which they are defined, rather than being
dynamically resolved, the concept of method overriding does not apply to static
methods in Java

Can we override Java main method?

No, because the main is a static method.


References
20

• Absolute JAVA Fifth Edition by Walter Savitch


• https://www.javatpoint.com/inheritance-in-java
• https://www.geeksforgeeks.org/inheritance-in-java/
• https://beginnersbook.com/2013/03/inheritance-in-java/
• https://www.slideshare.net/AdilAslam4/inheritance-and-its-
type-in-java
• https://www.javatpoint.com/method-overriding-in-java
• https://www.geeksforgeeks.org/super-keyword/
21

THANK YOU

You might also like