Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to have an 'empty' class that extends another class?

Tags:

java

Let's say I have one class Foo that has a bunch of logic in it and another class Bar which is essentially the same. However, as Foo and Bar are different (but related) entities I need the difference to be apparent from my code (i.e. I can tell whether an instance is a Foo or a Bar)

As I was whacking this together without much thought I ended up with the following:

public class Foo {
  /* constructors, fields, method, logic and what-not */
}

public class Bar extends Foo {
  /* nothing here but constructors */ 
}

Is this OK? Is it better to make Bar a composite class? e.g:

public class Bar {
  private Foo foo;

  /* constructors and a bunch of wrapper methods that call
     into foo */
}

Or even, while we're at it, something much more low-tech:

public class Foo {
  /* constructors, fields, method, logic and what-not */

  private boolean isABar; // Could be an enum
}

What do you think? How do you deal with these 'marker classes'?


As an example of how my code may wish to treat Foo and Bar differently, my code would need to be able to do stuff like List<Foo> and List<Bar>. A Foo couldn't go in a List<Bar> and vice versa.

like image 729
SCdF Avatar asked Sep 27 '08 05:09

SCdF


People also ask

Can an empty class be extended in Java?

Every class in java extends object class so an empty class will inherits all the functionalities of an Object class . Some other class an extend your empty class . It will have default constructor so obviously you can create a object of it .

What happens when a class extends another class?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

Can you extend a class that extends another class?

You can extend a class to provide more specialized behavior. A class that extends another class inherits all the methods and properties of the extended class. In addition, the extending class can override the existing virtual methods by using the override keyword in the method definition.

When should a class extend another class?

You extend a class when you want the new class to have all the same features of the original, and something more. The child class may then either add new functionalities, or override some funcionalities of the parent class.


2 Answers

In my opinion, it's best if Foo and Bar subclass off a common ancestor class (maybe AbstractFoo), which has all the functionality. What difference in behaviour should exist between Foo and Bar? Code that difference as an abstract method in AbstractFoo, not by using a if statement in your code.

Example: Rather than this:

if (foo instanceof Bar) {
    // Do Bar-specific things
}

Do this instead:

class Bar extends AbstractFoo {
    public void specialOp() {
        // Do Bar-specific things
    }
}

// ...
foo.specialOp();

The benefit of this approach is that if you need a third class, that's much like Foo but has just a little bit of difference, you don't have to go through all your code and add edit all the if statements. :-)

like image 64
Chris Jester-Young Avatar answered Oct 05 '22 23:10

Chris Jester-Young


It all depends on meaning of the Foo and Bar classes. What they represent, and what's their purpose. Please clarify.

I can imagine situations when each of your solutions and proposed solutions is the right one.

like image 39
Paweł Hajdan Avatar answered Oct 05 '22 23:10

Paweł Hajdan