Final method: a method which has final keyword in its
definition is called final method.
When a method should be defined as final?
If we do not want allow subclass to override super class
method and to ensure that all sub classes uses the same super class method
logic then that method should be declare as final method.
Final method cannot be overridden in subclass, violation
leads to CE:
What is the difference between private and final methods?
Private method is not inherited, where as final method is
inherited but cannot be overridden.
Can we overload final method in subclass?
Yes, we can overload final method in subclass.
Class Ex {
Final void m1 (int x) { }
}
Class Sa extends Ex{
Void m1(){}
}
Final Class:
A class which has final keyword in its definition is called
final class.
When a class should be defined as final?
In the below situations we must define class as final
- If we do not want define all methods as final
- If we do not want allow subclasses to extend our class functionality.
Sub class cannot be defined from final class, violation
leads to CE:
Final class Ex{
}
Final class members are not final until they are declared as
final members explicitly using final keyword.
No comments:
Post a Comment