What is overriding ?
Just simple definition is "Two or more methods with the same name and same parameter list then it said to be Method overriding" .Eg-1:
class A{
void show(){
System.out.println("Overridden method");
}
}
public class B extends A{
void show(){
System.out.println("Overriding method");
}
}
Eg-2:class A{
void show(int a){
System.out.println("Overridden method" + value);
}
}
public class B extends A{
void show(int a){
System.out.println("Overriding method"+ a);
}
}
#Rule-1
Method cannot be overridden in the same class because of ambiguity.
Eg-:
Invalid overriding
public class {
void show(){
System.out.println("Overriding method");
}
void show(){
System.out.println("Invalid method");
}
}
#Rule-2
In method overriding return type must be same except covariant return type.
Eg-1:
Valid overriding- Both A, B class show methods return type is same which is int.
class A{
int show(){
System.out.println("Overridden method");
}
}
public class B extends A{
int show(){
System.out.println("Overriding method");
}
}
|
Invalid overriding- Both A, B class method return type is not same.
class A{
int show(){
System.out.println("Overridden method");
}
}
public class B extends A{
float show(){
System.out.println("Overriding method");
}
}
What is covariant return type?
In method Overriding Java permits subclass type as a return type, this said to be Covariant
return type.
Eg:
class A{
// Some code
}
class B extends A{
// Some code
}
class C extends B{
// Some code
}
class MyClass{
A show(){
System.out.println("This reutrns the A class object");
}
}
public class Test extends MyClass{
B show(){
System.out.println("This reutrns the B class object which is subclass of A");
}
}
#Rule-3
In method overriding method can have same access modifier or less restrictive access modifier.
Note: Must not be the more restrictive access modifier.
Access modifier hierarchy from less restrictive to more restrictive.
Public --> Protected ---> No-name ---> Private
(Less) (More)
Eg-1:
Valid Overriding
class A{
protected int show(){
System.out.println("Overridden method");
}
}
public class B extends A{
public int show(){
System.out.println("Overriding method");
}
}
Eg-2
Invalid Overriding
class A{
// Here no-name - it means package level access modifier.
int show(){
System.out.println("Overridden method");
}
}
public class B extends A{
private int show(){
System.out.println("Overriding method");
}
}
#Rule-4
Methods which are declared with final keyword can't be Overridden because
"final" keyword(modifier) used for to prevent the Overriding.
Eg-1
Valid Overriding
class A{
int show(){
System.out.println("Overridden method");
}
}
public class B extends A{
final int show(){
System.out.println("Overriding method");
}
}
Eg-2
Invalid Overriding
class A{
final int show(){
System.out.println("Overridden method");
}
}
public class B extends A{
int show(){
System.out.println("Overriding method");
}
}
#Rule-5
Static methods cannot be Overridden because static members are not a part of an Object.
Eg-1
Invalid Overriding
class A{
static int show(){
System.out.println("Overridden method");
}
}
public class B extends A{
int show(){
System.out.println("Overriding method");
}
}
Eg-2Valid but not Overriding
class A{
static int show(){
System.out.println("Overridden method");
}
}
public class B extends A{
static int show(){
System.out.println("Overriding method");
}
}
Why static members are not a part of an object ?
For static members memory allocates while class loading time so Object is not needed to access the static members.
#Rule-5
Private method cannot be Overridden because private members cannot be inherited.
Eg-1
Valid but not Overriding
class A{
private int show(){
System.out.println("Overridden method");
}
}
public class B extends A{
private int show(){
System.out.println("Overriding method");
}
}
Why private method cannot be inherited?
Private methods only can accessible with in the class.
No comments:
Post a Comment