aspose file tools
The moose likes Beginning Java and the fly likes method overriding through interface VS method overriding through inheritence Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "method overriding through interface VS method overriding through inheritence" Watch "method overriding through interface VS method overriding through inheritence" New topic
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: method overriding through interface VS method overriding through inheritence
 
Similar Threads
Extends vs. Implement?
Method Overriding Interface vs. Class
Overriding vs Overloading
Abstract Class vs. Interface
Proxy Vs Decorative pattern