• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method overriding through interface VS method overriding through inheritence

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1)PLease explain me method overriding through interface VS method overriding through inheritence with code?

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic