• Post Reply Bookmark Topic Watch Topic
  • New Topic

method overriding through interface VS method overriding through inheritence  RSS feed

 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1)PLease explain me method overriding through interface VS method overriding through inheritence with code?

 
Greg Stevens
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!