Java
polymorphism invoking overridden method from a subclass.
Case1:-- which method cannot
override:-
Q.1 why the final method can not be overridden?
Ø If we don’t want a method to be overridden we declare it as final.
Q.2 why the static method can not be overridden?
Ø Method overriding
method hiding
Ø When you define a static method with the same signature
as a static method in base class it is known as method hiding
Q.3 privet method cannot be overridden?
Ø Privet method can’t be overridden as they are banded during compile time .there fore we can
not event override the privet method in a sub-class.
Example:--
class Test {
privet/static/ final void show
() {
System.out.println(“one”) }
}
class Best extends Test {
void show() {
System.out.println(“two”);
}
public static void
main(String args[]) {
Best b= new Best();
b.show();
}
}
Case 2:- overriding and synchronize | stritp method
v The presence of synchronizing/stript modifier with the method has no effect on the
rules of overriding. i.e it's possible that a synchronized /stript
method can override a non-synchronized strictfp one and vice-versa.
Example two in one:
class Test {
synchronized /stritfp void show
() {
System.out.println(“one”) }
}
class Best extends Test {
void show() {
System.out.println(“two”);
}
public static void
main(String args[]) {
Best b= new Best ();
b.show();
Test b= new Test ();
t.show();
}
}
Method overloading |
Method overriding |
1. Same name2. Within same class3. Different arguments(no of argument, sequence of argument, and type of arguments are different) |
4. Same name5. Different classes.6. Same arguments(no of argument, sequence of argument, and type of arguments same)7. Inheritance (IS-A) “extends” |
We can all parent class method in overridden method
using super keyword.
Example:--
class Test {
void show () {
System.out.println(“one”) }
}
class Best extends Test {
void show() {
super.show ();
System.out.println(“two”);
}
public static void
main(String args[]) {
Best b= new Best();
b.show();
}
}
0 Comments