SARAVANAN SIVAJI

Dedicated to JAVA Technologies

When to make a class, a subclass, an abstract class, or an interface?

Posted by SARAVANAN SIVAJI on October 6, 2010

Make a class that does not extend anything (other than Object) when your new class does not pass the IS-A test for any other type.

Make a subclass only when you need to make a more specific version of a class and need to override or add new behaviors.

Use an abstract class when you want to define a template for a group of subclasses, and you have at least some implementation code that all subclasses could use.  Make the class abstract when you want to guarantee that nobody can make objects of that type.

Use an interface when you want to define a role that other classes can play, regardless of where those classes are in the inheritance tree.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.