wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces" Watch "Interfaces" New topic
Author

Interfaces

Harsimrat Parmar
Greenhorn

Joined: Nov 26, 2011
Posts: 2
What is the difference between complete abstract class and an interface??
If we can have a completely abstract class what is the need of an interface??
Except for the multiple inheritance advantage??
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Harsimrat Parmar wrote:What is the difference between complete abstract class and an interface??
If we can have a completely abstract class what is the need of an interface??
Except for the multiple inheritance advantage??

Well, you've just come up with one answer yourself right there.

Other than that:
1. All methods defined in an interface are public.
2. All fields defined in an interface are public AND static.

I think a better question might be: given that you have interfaces, which are largely self-documenting, why would you ever write a completely abstract class?

Winston

Harsimrat Parmar
Greenhorn

Joined: Nov 26, 2011
Posts: 2
Winston Gutkowski wrote:
Well, you've just come up with one answer yourself right there.

Other than that:
1. All methods defined in an interface are public.
2. All fields defined in an interface are public AND static.

I think a better question might be: given that you have interfaces, which are largely self-documenting, why would you ever write a completely abstract class?

Winston



I totally agree with what you said but
What I wanted to know is.. Abstract classes can do all what an interface can do then why have interfaces?? Only for multiple inheritance ?
Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
Harsimrat Parmar wrote:What is the difference between complete abstract class and an interface??
If we can have a completely abstract class what is the need of an interface??
Except for the multiple inheritance advantage??


Hello Harsimrat Parmar

about your first Q What is the difference between complete abstract class and an interface well... there is very good example for that is Animal Class..

we know the "Abstract" means we just "declare our methods" means... we just "declare behavior of that class" which you may called "Reference Class" also.. so from that reference class we override methods in subclass that is we share methods

About Animal Class example Animal is Super class and Dog,Hippo,Tiger are sub class so there are certain behaviors which share by some class with other class so, that same behavior is in interface ... through that they share or behave same as other class does..

Interface:: i will tell you interface is just an Agreement between two classes just take example of Remote Control

there is TV is one class and User is another class so Remote Controls is media (interface) through we change our method('s) (channels, volume etc)

and methods which are present in interface it should be implemented in class who is implementing that interface.. may be it is sufficient for answer or may be not..

Thanking you.!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Harsimrat Parmar wrote:What I wanted to know is.. Abstract classes can do all what an interface can do then why have interfaces?? Only for multiple inheritance ?

No, they're also very useful for defining public types. An interface is pure; an abstract class is not (or need not be). Defining an interface requires that at least one class (or hierarchy) implements ALL its methods; you can define an abstract class for many reasons and in many ways, just one of which is a "skeleton implementation" class such as AbstractList.

Take the following method:It can take ANY kind of List (including ones that haven't yet been written) because it uses an interface; and furthermore it is guaranteed to work, providing (a) it compiles, and (b) the runtime class it receives doesn't contain any errors. Indeed, you can write the method without needing to know what kind of List it will actually receive.
If you instead coded it to take an abstract class (even if it was completely abstract), it would only be able to take an object descended specifically from that class (single inheritance, remember).

In fact, as in the case of AbstractList, what you'll find is that interfaces and abstract classes are often best written in pairs: an interface that defines the type (List) and an abstract class that provides a skeleton implementation (AbstractList).

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
An abstract class with all abstract methods might have fields. That is bad design, because it would be awkward to implement if the fields have private access, however.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces
 
Similar Threads
Core Java Releated Quesstion
difference between abstract class and an interface
what s the difference between abstract class and interface?
Abstract class and Interface
Abtract class and Interface