0

I read many links on Stack like this question and i decided to ask .

When i create a Abstract Shape Class in Java i think the Class must have a method to getArea()

I think there are two way .

public abstract class Shape
{
    public abstract double getArea();
}

When i create extended class from Shape , I redefine getArea() in extended classes.

So can i create this hierarchy with using Interface like this ?

public interface Contain
{
    public double getArea();
}

and create class like this :

public abstract class Shape implements Contain
{
...
}

Then redefine getArea() for extended class of Shape.

I am confused a little bit in there.

What is the best way to decide between Interface and Abstract Method in usage ?

Community
  • 1
  • 1
eemrah
  • 1,603
  • 3
  • 19
  • 37
  • 2
    My Question: How many times does this same question really have to be asked on this site. Seriously. – Hovercraft Full Of Eels Dec 25 '15 at 04:21
  • 1
    Please take your pic from this link to the [many, many similar questions on this site](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=java+interface+vs+abstract+class+site:http:%2F%2Fstackoverflow.com%2F). Google estimates about 4000 duplicates, but that's probably a bit of an over-estimate, but still it must be in the high hundreds or the thousands. – Hovercraft Full Of Eels Dec 25 '15 at 04:23
  • many , just many others . – eemrah Dec 25 '15 at 04:24
  • sorry but i got it this time – eemrah Dec 25 '15 at 04:25

2 Answers2

1

In a round-about way, yes their usages are "similar". More so is the comparison between an abstract class and an interface.

An interface is a set of predefined method stubs that can be implemented within a class and overridden as seen fit without inheritance.

An abstract class is a class with defined values and methods that cannot be instantiated and is for the purpose of inheriting/extending without other non-abstract classes.

For instance, you could have an abstract class called Animals with methods that are fully written and defined with variables and data members as well, yet you must first extend this Animals class with, say...Lion class that is not abstract to be able to instantiate and use these methods and data members.

With interfaces, I'll use an example of a radio. You could have an interface called RadioSystem that has method stubs for changing frequencies, switching from FM to AM and vice versa, etc., which is then implemented into another class, say...CarRadio so that it already has all of the methods stubs and predefined functionalities defined, then as the programmer you just need to write these methods in a way that best suits the class they're implemented by.

m_callens
  • 6,100
  • 8
  • 32
  • 54
0

I would decide between an Interface and Abstract Class with one Abstract Method by thinking about whether the class has any base implementations. These base implementations are possible only with an Abstract Class, so I'd choose an Abstract Class. Otherwise, I'd choose an Interface. It depends on the domain you are trying to model.

dseibert
  • 1,319
  • 9
  • 19