aspose file tools*
The moose likes Beginning Java and the fly likes Interfaces and abstract classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces and abstract classes" Watch "Interfaces and abstract classes" New topic
Author

Interfaces and abstract classes

Yamuna Pattathil
Greenhorn

Joined: Dec 03, 2000
Posts: 19
Could somebody provide a definition for interfaces and abstract classes. I know how to work with it but was unable to provide a definition for both when asked(in an interview). Be prepared for such questions when u face a java interview.
Thanx in advance
Yamuna
Charlotte Hughes
Greenhorn

Joined: Nov 29, 2000
Posts: 11
An Abstract class is a class which declares the existence of methods but not the implementation and can contain member variables and non-abstract methods.
An Interface can define only static final member variables and can only have abstract methods. An interface gives the illusion of bending the Java single inheritance rule. While a class definition can extend only a single class, it can implement as many interfaces as needed.
Hope this helps! Thanks for the interview tip!
karai baskar
Greenhorn

Joined: Nov 29, 2000
Posts: 17
hai i hope this will help u
An abstract class cannot be instantiated. Only its subclasses can be instantiated. You indicate that a class is abstract with the abstract keyword like this:
public abstract class Container extends Component {
Abstract classes may contain abstract methods. A method declared abstract is not actually implemented in the current class. It exists only to be overridden in subclasses. It has no body. For example,
public abstract float price();
Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most of them do.
Each subclass of an abstract class must override the abstract methods of its superclasses or itself be declared abstract.
For more details, see section 8.1.2.1 of the Java Language Specification.
What's an interface?
An interface is an idea taken from Objective C. It describes the public methods that a class implements and their calling conventions without saying anything about how those methods are implemented. It is the responsibility of each class that implements an interface to provide code to handle the cases where the methods of the interface are called.
For example suppose you're writing an inventory database. The inventory may include many different items of many different types and classes. However each item in the warehouse needs to be able to tell you its price. Normally you would implement this by having each class extend a common superclass. However that's not always convenient. Instead you can declare an interface called Price with a price() method like this:
public interface Price {
public float price();
}
Any class which implements the Price interface must contain a method with the signature public float price(). The code of the price() method is included separately in each separate class which implements Price, not in the Price interface itself.
Different classes in your warehouse can each implement the Price interface like this:
public class Monopoly extends BoardGame implements Price {
// other methods
public float price() {
return 14.95;
}
}
When other code is passed an object, it can test whether the object implements Price with the instanceof operator. For example,
if (o instanceof Price) System.out.println("Subtotal is " + o.price());
In fact, interfaces can be used to tag objects. The java.rmi.Remote interface declares no methods. Its sole purpose is to indicate that an object is a remote object. In general, sub-interfaces of java.rmi.Remote will declare remote methods, however. For example,
public interface Hello extends java.rmi.Remote {
public String sayHello();
}
public class HelloImpl extends UnicastRemoteServer implements Hello {
public String sayHello() {
return "Hello";
}
}
bye
knbaskar@usa.net
eric moon
Ranch Hand

Joined: Nov 26, 2000
Posts: 133
[Well, I just realized I wasn't answering the question you asked, but I'll leave this in anyway.....]
The interesting thing about an interface is that a class can be referred to by its interface. For instance, if you have a bunch of completely different classes that all implement interface "chewable" (containing method "chewOnMe()"):
Vector chewables = new Vector();
and fill it with all different kinds of objects, as long as they are chewable:
Candy starburst = new Candy();
RubberToy squeaker = new RubberToy();
VeryUnfamiliarConcept javaInterface = new VeryUnfamiliarConcept();
MouthCancerSource skoal = new MouthCancerSource();
chewables.add(starburst);
chewables.add(squeaker);
chewables.add(javaInterface);
chewables.add(skoal);
Then you can chew on any of them, by casting them to chewable:
for (int i = 0; i < 10; i++) {
((chewable)chewables.get(i)).chewOnMe();
}
An interface is much "looser" than inheritance. The only similarity that objects implementing an interface have, is the ablility to deal with the method calls in the interface.
Now here's something I'm not sure of: can I have an array of chewables???
Hmmmmm....
[This message has been edited by eric moon (edited December 16, 2000).]


<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Interfaces and abstract classes