| Author |
method overriding through interface VS method overriding through inheritence
|
Satyajeet Kadam
Ranch Hand
Joined: Oct 19, 2006
Posts: 202
|
|
Q1)PLease explain me method overriding through interface VS method overriding through inheritence with code?
|
 |
Greg Stevens
Ranch Hand
Joined: Jul 23, 2009
Posts: 41
|
|
Classes that implement interfaces don't override methods they implement them. There was never
a previous definition of the method to override. They are providing the first definition. This mechanism
of stating that a class will have a method but not providing a default implementation is useful if a set of
objects all do a certain activity, but there is no default way for doing it. They each do it their own way.
|
 |
Greg Stevens
Ranch Hand
Joined: Jul 23, 2009
Posts: 41
|
|
Also interfaces can be used to describe a class that has two or more different sets of behavior. A class
can only have one superclass (inheritance), but it can implement the behavior of any number of interfaces.
Perhaps it will implement seemingly unrelated behavior that would never be described through inheritance
but can be expressed with interfaces:
|
 |
Greg Stevens
Ranch Hand
Joined: Jul 23, 2009
Posts: 41
|
|
Concerning inheritance and overriding part of your question
You should read Sun's bicycle inheritance tutorial. Notice that the more specific type of bicycle class still uses most of the superclass's
definition (fields and methods) but may override a method or initialize some of the instance variables to different values.
|
 |
 |
|
|
subject: method overriding through interface VS method overriding through inheritence
|
|
|