Skip to main content

Abstract class and interface

1 reply [Last post]
Joined: 2008-03-08

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-03-05

Interface can't have any logic. It has only constants and unimplemented methods. It's used to define, which methods are necessary for some reason. For example interface Comparable is used for comparing two objects, so necessary method is compareTo().

Abstract class can have some logic, which is same for all subclasses. For example you have abstract class Shape2D, where you have abstract method countArea() because it's different for all subclasses and abstract method getShapeName() which returns name of this shape (for example rectangle). But you can do normal method toString(), which can be same for all subclasses.
public String to String(){
return getShapeName() + " with area " + countArea() + " m2."

I don't think this forum is ideal for your question ...