0

i have an abstract class,this class is extended in her subclasses:

  • i implementend one method on this abstract class and i made the other method abstracts
  • the implemented method is a general method that every subclass object has to access on it.So i decided to implement it on the abstract class,avoid implementing the same method on each subclass.

little example:

public abstract class Foo{

           //plus constructor and other stuff.

        public abstract void differentTypeOfImplementation();

        public void doSomething(Foo foo){
                        //do something with the generic Foo object passed
        }
}

i want your opinion on this type of implementation,

regards.

1
  • Looks like a normal idiom. In general, though, StackOverflow is not suited for asking for opinions, so this question will probably get closed. I think there's a "code review" site on Stack Exchange that might be more suitable. Commented Apr 11, 2014 at 18:26

2 Answers 2

2

This question is probably too open ended, but your solution is perfectly fine.

The alternative is that you can make an Interface with differentTypeOfImplementation(), and then a utility class with doSomething. That way, your subclasses can also extend from other classes. However, if subclasses may occasionally override doSomething, or if doSomething require accessing internal states of the object, then what you have is perfectly valid.

Sign up to request clarification or add additional context in comments.

Comments

0

Implementing a method in an abstract class is very much valid and acceptable design. If this method implementation is necessary for all its subclasses then this is the way to go. In your example however - the signature of the method makes it little fishy - it looks like you are not using the super class state in any way . That means you could as well declare this method as static.

2 Comments

excuse me,i don't understand why i have to declare it static? can you be more specific?
@CiMat - depending upon what you exactly intend to do in your method implementation - if you are not accessing any instance level state ( members or other non static methods ) then you can declare it static. I am not saying you have to. It depends.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.