Over-Riding:- An
override is a type of function which occurs in a class which inherits from
another class. An override function “replaces” a function inherited from the
base class, but does so in such a way that it is called even when an instance
of its class is pretending to be a different type through polymorphism.
In simple words, method in base class is overridden in child
class. The key benefit of overriding is the ability to define behavior that’s
specific to a particular subclass type. The code snippet below should explain
things better.
Sample Program:-
Sample Program:-
/**
*
*/
/**
* @author Dixit
*
*/
public class SampleProgram {
/**
* @param args
*/
public static void main(String[] args) {
Car a = new Car();
Car b = new Porche();// Car reference ,but a Porche object
a.start();// Car version of start method called
b.start();// Porche version of start method called
}
}
class Car {
public void start() {
System.out.println("Generic method to start the car in Base class");
}
}
class Porche extends Car {
public void start() {
System.out.println("Porche method to start the car in child class");
}
}
Output
Generic method to start the car in Base class
Porche method to start the car in child class
The rules for overriding are:-
-
The argument list must exactly
match that of the overridden method. If they
don't match, you can end up with an overloaded method
you didn't intend.
-
The return type must be the same
as, or a subtype of, the return type declared
in the original overridden method in the superclass.
-
The access level can't be more
restrictive than the overridden method's.
-
The access level CAN be less
restrictive than that of the overridden method.
-
Instance methods can be overridden
only if they are inherited by the subclass.
A subclass within the same package as the instance's
superclass can override
any superclass method that is not marked private or
final. A subclass in a
different package can override only those non-final
methods marked public
or protected (since protected methods are inherited by
the subclass).
-
The overriding method CAN throw
any unchecked (runtime) exception,
regardless of whether the overridden method declares
the exception.
-
The overriding method must NOT
throw checked exceptions that are new
or broader than those declared by the overridden
method. For example, a
method that declares a FileNotFoundException cannot be
overridden by a
method that declares a SQLException, Exception, or any
other non-runtime
exception unless it's a subclass of
FileNotFoundException.
-
The overriding method can throw
narrower or fewer exceptions. Just because
an overridden method "takes risks" doesn't
mean that the overriding subclass'
exception takes the same risks. Bottom line: an
overriding method doesn't
have to declare any exceptions that it will never
throw, regardless of what the
overridden method declares.
-
You cannot override a method marked
final.
-
You cannot override a method
marked static.
-
If a method can't be inherited,
you cannot override it.
Over-Loading:- Overloading
is the action of defining multiple methods with the same name, but with
different parameters. It is unrelated to either overriding or polymorphism.
Functions in java could be overloaded by two mechanisms ideally:
-
Varying the number of arguments
-
Varying the Data type
The rules are simple:-
-
Overloaded methods MUST change the
argument list.
-
Overloaded methods CAN change the
return type.
-
Overloaded methods CAN change the
access modifier.
-
Overloaded methods CAN declare new
or broader checked exceptions.
-
A method can be overloaded in the same
class or in a subclass. In other words,
if class A defines a
doStuff(int i) method, the subclass B could define a
doStuff(String
s) method without overriding the superclass version that
takes an int. So two
methods with the same name but in different classes
can still be considered
overloaded, if the subclass inherits one version of the
method and then declares another overloaded version in its class
definition.
Sample Program:-
Happy Learning :)
/**
*
*/
/**
* @author Dixit
*
*/
public class CalulateSum {
void sum(int a, int b) {
System.out.println("Integer Sum:" + (a + b));
}
void sum(double a, double b) {
System.out.println("Double Sum:" + (a + b));
}
/**
* @param args
*/
public static void main(String[] args) {
CalulateSum calulateSum = new CalulateSum();
calulateSum.sum(3, 4);
calulateSum.sum(3.2, 4.8);
}
}
Output
Integer Sum:7
Double Sum:8.0
Happy Learning :)
No comments:
Post a Comment