Results 1 to 10 of 10
Thread: Interfaces
- 01-20-2010, 03:52 AM #1
Member
- Join Date
- Jan 2010
- Location
- Wisconsin
- Posts
- 20
- Rep Power
- 0
Interfaces
Hi, could someone explain to me the difference between a regular class and an interface and why you would use it. i know there is a million things on this already if you Google it. But, I have been looking and I'm not understanding it fully. I also need to know about abstract classes and the difference between them to. Maybe this is to much to ask? Maybe you have some links to somewhere that explains it really well?
Thank you!
From what I am reading, an abstract class seams the same as using a superclass and sub classes in that it is used to pass methods down to subclasses. Thants why I am confused. Im trying to figure out the real differances between all of these.Last edited by justin1980; 01-20-2010 at 03:57 AM. Reason: Add more
- 01-20-2010, 05:17 AM #2
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
ok lets break it down.
a regular class is a class where every method can be implemented
an interface is like a container which just contains the method signatures but are not implemented.These are useful when you write a large amount of code where different classes keep using a similar type of method signature. So we put the method signature inside in an interface and make it implement to the desired class.
Abstract class is a mix of interface and regular class, as it has both the features in it. It can just declare the method and also have implemented methods.
These are just a basic definitions. if your interested
Definitions
Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
When to use
interface vs abstract class : Java Glossary
Hope it helped
-
There's so much to this subject that to try to summarize it in the confines of a forum is not doing it justice. I strongly believe that your best bet is to read aggressively (as opposed to passively) as much as you can about these beasts and try to use them. I'll tell you this: I didn't fully appreciate the power of interfaces until I started studying design patterns such as the observer pattern (which Swing coding makes much use of). Once I learned this, the light clicked on in my eyes, and I thought, "Interfaces, .... oh yeah...[cue bright sun light shining through and swelling music], so THAT's their power..."
Hopefully with time, you'll see the light. Praise Java, amen.
- 01-20-2010, 05:26 AM #4
Member
- Join Date
- Jan 2010
- Location
- Wisconsin
- Posts
- 20
- Rep Power
- 0
haha I am studying this because I started an advanced Java class today and its called Advanced Java and Design Patterns lol
He wants us to do a speach on them next week!
- 01-20-2010, 09:35 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
A quick summary, because that's all you can possibly hope for here:
An interface is contract. It essentially says that anything that implements that interface will provide this functionality (ie these methods). Look at Collections (ie List) to see that in action.
An abstract class provides some pre-done functionality, usually stuff that is likely to be standard for all (or most) extending classes, but also provides additional methods which will (as with an interface) need to be implemented by any child class. So it's a contract with some predefined implementations.
A concrete class is the only one that can be instantiated because it fulfills all its contractual obligations.
- 02-05-2010, 03:17 PM #6
- 02-05-2010, 08:49 PM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
The strong point of interfaces is hiding implementation, and that makes your code easily maintained. By using interfaces, you can write methods, that survive intact, if you do extensive changes to the class, that implements them. Consider:
Now, you write up your "real" class:Java Code:public interface SomeStruct { public abstract void add(Object o); public abstract Object remove(Object o); }
Now, you make up another class that uses a different structure to hold data:Java Code:public class MyStruct implements SomeStruct { private Object[] contents; //initialized with constructor public void add(Object o) { //add the object into the array } public Object remove(Object o) { //remove and return object from array } }
Now, you have 2 classes, that use different methods for storage, but to the outside observer act the same! So instead of:Java Code:public class AnotherStruct implement SomeStruct { private Vector contents; public void add(Object o) { //insert object into vector } public Object remove(Object o) { //remove and return object from vector } }
You can simply write:Java Code:public void doSomething(MyStruct m) { //do something with the struct }
This function will work, if you pass it a MyStruct or AnotherStruct object the same, because they both implement the same set of methods.Java Code:public void doSomething(SomeStruct s) { //do something with the struct }
Abstract classes are similar to interfaces, but they allow you to fully designate a method(or methods), that it's subclasses will inherit, while interfaces only allow method signatures.
- 02-10-2010, 11:47 AM #8
Interfaces are used for multiple Inheritance.
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
- 02-10-2010, 12:00 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Java does not have multiple inheritance.
- 02-10-2010, 12:04 PM #10
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
Similar Threads
-
Interfaces
By jon80 in forum New To JavaReplies: 2Last Post: 05-03-2008, 09:57 PM -
interfaces..
By sireesha in forum New To JavaReplies: 5Last Post: 01-16-2008, 05:52 PM -
Interfaces
By Kavana Krishnappa in forum New To JavaReplies: 7Last Post: 12-11-2007, 04:28 PM -
Interfaces
By imran_khan in forum New To JavaReplies: 5Last Post: 07-30-2007, 08:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks