0% found this document useful (0 votes)
35 views13 pages

Java Overloading vs Overriding Explained

The document discusses the concepts of overloading and overriding in Java. It provides examples to differentiate between overloading and overriding. Overloading occurs within the same class and involves methods with the same name but different parameters. Overriding occurs between superclasses and subclasses, where a subclass method has the same name and parameters as a superclass method. The key differences are that overloading is resolved at compile time while overriding is resolved at runtime based on the object type.

Uploaded by

Balaji Gaikwad
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)
35 views13 pages

Java Overloading vs Overriding Explained

The document discusses the concepts of overloading and overriding in Java. It provides examples to differentiate between overloading and overriding. Overloading occurs within the same class and involves methods with the same name but different parameters. Overriding occurs between superclasses and subclasses, where a subclass method has the same name and parameters as a superclass method. The key differences are that overloading is resolved at compile time while overriding is resolved at runtime based on the object type.

Uploaded by

Balaji Gaikwad
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

CO_IF_22412_UO2

Chetashri Bhusari, Lecturer, Vidyalankar Polytechnic

Date: 02 February 2021

MSBTE LEAD: Learning at your Doorstep


Unit 03 :
Inheritance ,Interface and
package

Written by

Chetashri Sachin Bhusari


Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Unit Outcome 2 :
Differentiate between overloading
and overriding for given example

Written by

Chetashri Sachin Bhusari


Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Learning Outcome 2a : Students
should understand Differentiate
between overloading and overriding

Written by

Chetashri Sachin Bhusari


Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
What we will learn today

1. Concept of overloading and overriding Key takeaways


2. Examples Concept of overloading and overriding
.

Chetashri Sachin Bhusari


Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai

Page 5 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Map

Page 6 Maharashtra State Board of Technical Education 2 Feb 2021


Learning Objective/ Key learning

► Understand concept of final keyword


► Understand concept of final variable
► Understand concept of final class
► Understand concept of dispatch method
► Understand concept of super key
► Example

Page 7 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Explanation: Final Keywords in Java

Page 8 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Explanation: Final Variable

► Final variable: the value of a final variable cannot be changed.


► final variable behaves like class variables and they do not take any space on individual objects of the
class.
► Example of declaring final variable: final int size = 100;
► When a variable is declared with final keyword, its value can’t be modified, essentially, a constant.
This also means that you must initialize a final variable.
► If the final variable is a reference, this means that the variable cannot be re-bound to reference
another object.
► Internal state of the object pointed by that reference variable can be changed i.e. you can add or
remove elements from final array or final collection. It is good practice to represent final variables in
all uppercase, using underscore to separate words.

Page 9 Maharashtra State Board of Technical Education 2 Feb 2021


Program

10

Page 10 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Explanation: Final Method

► final method: making a method final ensures that the functionality defined in this method will never
be altered in any way, i.e. a final method cannot be overridden.
► Syntax:

final void findAverage()


{
//implementation
}

11

Page 11 Maharashtra State Board of Technical Education 2 Feb 2021


Program

class Super class A


{ {
int x; int i;
Super (int x) A(int a, int b)
{ {
this.x=x; i=a+b;
} }
void display() void add()
{ {
[Link](“Sum of a & b is=” +i);
[Link](“Super x=”‖+x); }
}
}
}
Class Sub extends Super class B extends A
{ {
int y; int j;
Sub( int x, int y) B(int a, int b, int c)
{ {
super (x); super(a,b); j=a+b+c;
this.y=y; }
} void add()
void display() {
{ [Link]();
[Link](“Super x=”‖ +x); [Link](“Sum of a, b & c”+j);
[Link](“Sub y=‖” +y); }
} } 12
} class MethodOverride
class overrideTest {
{ public static void main(String args[])
public static void main(String arg[]) {
{ B b=new B(10,20,30);
Sub s1= new Sub(100,200); [Link](); [Link]();
}
}
}
}
Page 12 Maharashtra State Board of Technical Education 2 Feb 2021
Difference between Method overloading and overriding

Method Overloading Method Overriding

It happens within the same It happens between super class


class and subclass
Method signature should not be Method signature should be
same same.
Method can have any return Method return type should be
type same as super class method
Overloading is early binding or Overriding is late binding or
static binding dynamic binding
They have the same name but, have They have the same name as a superclass
different parameter lists, and can have method. They have the same parameter list
different return types. as a superclass method. They have the same
return type as a superclass method
It is resolved at compile time It is resolved at runtime.

Inheritance does not blocked Method overriding blocks the


by method overloading. inheritance.
One method can overload Method overriding can be done
only once per method in the sub class.
unlimited number of times.

Page 13 Maharashtra State Board of Technical Education 2 Feb 2021

You might also like