0

Shape.java

public interface Shape {
    String draw();

    void calcSomething();
}

Square.java

public class Square implements Shape {
    @Override
    public String draw() {
        return "SQUARE";
    }

    @Override
    public void calcSomething() {

    }
}

When I implement an interface, the method that I implemented must be public.

I want to make draw() public method but calcSomething() private or protected.

I've gone through Interfaces in Java: cannot make implemented methods protected or private . And there no straight way to do this

So instead of using interface I'm planning to use abstract class

Shape.java using abstract class

public abstract class Shape {
    abstract public String draw();

    abstract protected void calcSomething();
}

Square.java that implements Shape abstract class

public class Square extends Shape {
    @Override
    public String draw() {
        return "SQUARE";
    }

    @Override
    protected void calcSomething() {

    }
}

Which one should I choose. Should I use interface and make the calcSomething() public or Should I use abstract class and make the calcSomething() protected

Community
  • 1
  • 1
A0__oN
  • 8,740
  • 6
  • 40
  • 61
  • opinion: i´d be using the second one in order to be able to store the shape coordinates itself in a `List` or something like that. But an abstract class would only make sense if you´d want to store information for this `Shape`, whilst letting space for `abstract` implementations for the inheriting classes. As a sidenode, if i´d be reading the method `draw`, i´d rather expect it to draw something instead of just returning a `String`. I might consider to rename this method to represent what it is really doing. – SomeJavaGuy May 23 '16 at 08:02
  • Why is `calcSomething` private if it is in the interface? – Reut Sharabani May 23 '16 at 08:04
  • I've different class that implements Shape. All of them use same method name to calculate something. I just wanted to add that method on interface. @ReutSharabani. I may be wrong to think like this. Please correct me if i'm wrong – A0__oN May 23 '16 at 08:07
  • @KevinEsche This is just an example. Can you send me edit request. – A0__oN May 23 '16 at 08:07
  • @AmanTuladhar This sounds wrong. You need to override the method. Why make it private? You're writing an implementation to your API (interface). – Reut Sharabani May 23 '16 at 08:08
  • @ReutSharabani I'll keep that in mind and try to do this other way. But i'll keep this question open for couple of days to see others view. Thanks – A0__oN May 23 '16 at 08:12

1 Answers1

2

Quoting The Java™ Tutorials - What Is an Interface?:

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world.

In order for the outside world to access the methods, they must be public.

If you don't want the method to be public, i.e. callable from the outside world, then it doesn't belong in the interface.

If you want calcSomething() to be callable from other code in Shape, then ... Oops, there is no other code in Shape. It's an interface.
Ok, if you want calcSomething() to be callable from other code in Square, you need it to be an abstract (or stub) method. This is for example how the template method pattern works.

Andreas
  • 154,647
  • 11
  • 152
  • 247