开发者

Java Interfaces and Abstract class [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Why are interfaces preferred to abstract classes?

Abstract class and Interface class?

Please give a real world example for the use of Interfaces. Also, what are the differences between an Abstract Class and an Interface. I mean, what are the benefits of Interfaces over abstract class, because Abstract Classes can also be used for the same purpose as the Interfaces. Then why Interfaces are so important?

Thanks for your comments 开发者_如何学Pythonguys, but still I didn't get answer for my question. I have asked for a "REAL WORLD EXAMPLE" of java interfaces. In real world, like in our daily life, where we use java interfaces. I think i am clear this time.


"A Java interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior. "

Take a look at these JAVA Interface tutorials:

  • http://download.oracle.com/javase/tutorial/java/concepts/interface.html
  • http://www.iam.ubc.ca/guides/javatut99/java/more/interfaceDef.html


Interfaces simply define the contract (API) of the class. In other words, it simply states which methods you MUST implement if your class implements the interface.

With Abstract classes, you can implement functionality. This is usually done when multiple classes have similar functionality, so the implementation only needs to be done once.

Another differnce between the two is that a class my implement many interfaces, but can extend only one abstract class (multiple inheritance). This is because interfaces don't define implementation. If you could extend multiple abstract classes, the JVM would not know which implementation from which abstract class to use.

Example of using an interface:

public interface Bouncable {
    public void bounce();  // no implementation
}

public class Ball implements Bouncable {
    // now you must provide the implementation
    public void bounce() {
        System.out.println("I can bounce!");
    }
}

But let's say you have more than one object that can bounce, and you don't want to write the implementation more than once.

Example of using abstract class:

public abstract class BouncableObject {
    // you can provide implementation
    public void bounce() {
        System.out.println("I can bouce too!");
    }

    // or not if you define it abstract
    public abstract void sayMyName();
}

// you don't need to implement bounce()
public class Ball extends BouncableObject {
    // but you have to give this implementation
    public void sayMyName() {
        System.out.println("I am a ball");
    }
}

public class RubberChicken extends BouncableObject {
    public void sayMyName() {
        System.out.println("I am a rubber chicken");
    }
}

And now both Ball and RubberChicken can call bounce()


In java, the two concepts abstract class and interface sounds similar. But,

(1) In interface you can only declare the members, you can't define anything inside the interface In abstract class can have both definition and declaration.

(2) Remember "A class can extend only one class where as it can implement more than one interface" You can't extend more than one class in java where as you can implement any number of interfaces. This is one of the advantage of Interfaces over the Abstract Classes.

(3) A class which extends the abstract class should define all the members from the abstract class. If you don't want to define all of them you need to declare the sub class also as abstract class. Where as in Interfaces, if you implement an interface you can use what ever members you want from that interface, so need of declaring the class as abstract or any thing else. This is another advantage of using Interfaces rather than abstract class.


Abstract class : Abstract classes permit declaration of classes that define only part of an implementation, leaving the subclasses to provide the details.

In the class: Implementation of common state and behaviour that will be inherited by subclasses. Declaration of generic behaviours that subclasses must implement

An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation. for any abstract methods declared in its superclasses must be declared as an abstract class. An abstract class cannot be instantiated, however references to an abstract class can be declared.

Interface:

An interface is like a class that defines only abstract methods. Unlike a class, an interface cannot include implementation (variables and method bodies). An interface is typically intended to be implemented by several classes

A class that claims to implement an interface must provide an implementation of every method defined within the interface. A class can implement many interfaces. An Interface can extend an existing interface.

Have a look at the lectures here: http://www.cs.auckland.ac.nz/courses/compsci230s2c/lectures/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜