Overridding
Definition
If subclass (child class) has the same method as
declared in the parent class, it is known as Method overriding.
If subclass provides the specific
implementation of the method that has been provided by one of its parent class,
it is known as Method Overriding.
Advantage of Java Method Overriding
·
Method Overriding
is used to provide specific implementation of a method that is already provided
by its super class.
·
Method Overriding
is used for Runtime Polymorphism
Rules
for Method Overriding
1.
method must have same
name as in the parent class
2.
method must have same
parameter as in the parent class.
3.
must be IS-A
relationship (inheritance).
Exception Handling with Method Overriding
There are many rules if we talk about methodoverriding with exception handling. The Rules are as follows:
1. If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
2. If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.
Understanding the problem without method
overriding
Let's understand the problem that we may face in the
program if we don't use method overriding.
public class Bank {
public int getRateOfInterest() {
return
10;
}
}
public class SBIBank extends Bank {
}
public class TestMain {
public
static
void
main(String[] args) {
SBIBank s = new SBIBank();
System.out.println("SBI
Bank Rate of Interest: " + s.getRateOfInterest());
}
}
Problem is that I have to provide a specific
implementation of getRateOfInterest() method in subclass that is why we use method
overriding.
Output
SBI Bank Rate of Interest: 10
____________________________________________________________
Real world example of Java Method Overriding
Suppose We have one Bank class which is having on method getRateOfInterest method.
public class Bank {
public int getRateOfInterest() {
return 10;
}
}
public class SBIBank extends Bank {
public int getRateOfInterest() {
return 8;
}
}
public class ICICIBank extends Bank {
public int getRateOfInterest() {
return 9;
}
}
public class AxisBank extends Bank {
public int getRateOfInterest() {
return 11;
}
}
public class TestMain {
public static void main(String[] args) {
Bank s = new SBIBank();
Bank i = new ICICIBank();
Bank a = new AxisBank();
System.out.println("SBI Rate of Interest: " + s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: " + i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: " + a.getRateOfInterest());
}
}
Output
SBI Bank Rate of Interest: 8
ICICI Bank Rate of Interest: 9
AXIS Bank Rate of Interest: 11
Notes:- Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.
In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
______________________________________________________________
Runtime Polymorphism with
data member
Method
is overridden not the data members, so runtime polymorphism can't be achieved
by data members.
In
the example given below, both the classes have a data member speed limit, we
are accessing the data member by the reference variable of Parent class which
refers to the subclass object. Since we are accessing the data member which is
not overridden, hence it will access the data member of Parent class always.
Rule: Runtime
polymorphism can't be achieved by data members.
public class Bank {
public int rateOfInterest = 10;
}
public class SBIBank extends Bank {
public int rateOfInterest = 8;
}
public class ICICIBank extends Bank {
public int rateOfInteres= 9;
}
public class AxisBank extends Bank {
public int rateOfInterest= 11;
}
Output
SBI Bank Rate of Interest: 10
ICICI Bank Rate of Interest: 10
AXIS Rate of Interest: 10
___________________________________________________________________________
___________________________________________________________________________
Overloading
Definition:-
If a class have multiple methods by same name but
different parameters, it is known as Method Overloading.
Advantage of Java Method Overloading
· If we have to perform only one operation, having same name of the methods increases the readability of the program.
· Method Overriding is used for Compile time Polymorphism
Rules for Method Overloading
1. method must have same name
2. method must have same return type
There are two ways to overload the method in java
- By changing number of arguments
- By changing the data type
1)
Example of Method Overloading by changing the no. of arguments
In this example, we have created two overloaded
methods, first sum method performs addition of two numbers and second sum
method performs addition of three numbers.
public class Calculation {
void sum(int a, int b) {
System.out.println("Sum is " + (a
+ b));
}
void sum(int a, int b, int c) {
System.out.println("Sum is " + (a
+ b + c));
}
public static void main(String[] args) {
Calculation obj = new Calculation();
obj.sum(10, 10, 10);
obj.sum(20, 20);
}
}
Output:
Sum is 30
Sum is 40
2)Example
of Method Overloading by changing data type of argument
In this example, we have created two overloaded
methods that differs in data type. The first sum method receives two integer
arguments and second sum method receives two double arguments.
public class Calculation {
void sum(int a, int b) {
System.out.println("Sum is " + (a
+ b));
}
void sum(double a, double b) {
System.out.println("Sum is " + (a
+ b ));
}
public static void main(String[] args) {
Calculation obj = new Calculation();
obj.sum( 10, 10);
obj.sum(20.5, 20.6);
}
}
Output
Sum is 20
Sum is 41.1
Let's see the difference between
Overloading & Overriding
Method Overloading
|
Method Overriding
|
|
1
|
In Same class
|
More than one
classes required
|
2
|
Method overloading is used to increase
the readability of the program
|
Method overriding is used to provide the
specific implementation of the method that is already provided by its super
class.
|
3
|
Method overloading is performed within a
class.
|
Method overriding occurs in two classes
that have IS-A relationship.
|
4
|
In case of method overloading parameter
must be different
|
In case of method
overriding parameter must be same.
|
5
|
||
Static Binding and Dynamic Binding
Connecting a method call to the method body is known
as binding.
There are two types of binding
1.
Static binding (also
known as early binding).
2.
Dynamic binding (also
known as late binding).
Understanding
Type
Let's understand the type of instance.
1) Variables have a type
Each variable has a type; it may be primitive and
non-primitive.
1.
int data=30;
Here data variable is a type of int.
2) References have a type
1.
class Dog{
2.
public static void main(String args[]){
3.
Dog d1;//Here d1 is a type of Dog
4.
}
5.
}
3) Objects have a type
An
object is an instance of particular java class,but it is also an instance of
its superclass.
1.
class Animal{}
2.
3.
class Dog extends Animal{
4.
public static void main(String args[]){
5.
Dog d1=new Dog();
6.
}
7.
}
Here d1 is an instance of Dog class, but it is also an
instance of Animal.
Static
binding:-When type of the
object is determined at compiled time (by the compiler), it is known as static
binding.
If there is any private, final or static method in a
class, there is static binding.
Example
of static binding
1.
class Dog{
2.
private void eat(){System.out.println("dog is eating...");}
3.
4.
public static void main(String args[]){
5.
Dog d1=new Dog();
6.
d1.eat();
7.
}
8.
}
Dynamic
binding:- When type of the
object is determined at run-time, it is known as dynamic binding.
Example of dynamic binding
1.
class Animal{
2.
void eat(){System.out.println("animal is eating...");}
3.
}
4.
5.
class Dog extends Animal{
6.
void eat(){System.out.println("dog is eating...");}
7.
8.
public static void main(String args[]){
9.
Animal a=new Dog();
10. a.eat();
11. }
12. }
Output:dog is eating...
In the above example object type cannot be determined by the compiler, because the instance of Dog is also an instance of Animal.So compiler doesn't know its type, only its base type.
Can we override static method?
No, static method cannot be
overridden.
Why we cannot override static method?
Because static method is bound with class, whereas
instance method is bound with object. Static belongs to class area and instance
belongs to heap area.
Can we override java main method?
No, because main is a static method.
Why
Method Overloaing is not possible by changing the return type of method?
In java, method overloading is not possible by
changing the return type of the method because there may occur ambiguity. Let's
see how ambiguity may occur:
because there was problem:
public class Calculation {
public int sum(int a, int b) {
return
(a + b);
}
public double sum(int a, int b) {
return
(a + b) ;
}
}
Note:- Duplicate method error will
come at compile time
Can we overload main() method?
Yes, by method overloading. You can have any number of
main methods in a class by method overloading. Let's see the simple example:
public class TestMain {
public static void main(String[] args) {
}
public static void main(int a) {
System.out.println(a);
}
}
0 comments: