Working with Related Classes in Java

Many of the classes you'll see in Java have created objects that stand on their own. However, the real power of object-oriented programming lies in its ability to create classes that describe objects that are closely related to each other.



For example, baseballs are similar to softballs; both are specific types of balls. Each has a diameter and a weight; both can be thrown, caught, or hit. However, they have different characteristics that cause them to behave differently.



If you're creating a program that simulates the way baseballs and softballs work, you need a way to represent these two types of balls. One option is to create separate classes to represent each type of ball. These classes are similar, so you can just copy most of the code from one class to the other.



Another option is to use a single class to represent both types of balls. Then, you pass a parameter to the constructor to indicate whether an instance of the class behaves like a baseball or like a softball.



Java has two object-oriented programming features that are designed specifically to handle classes that are related like this: inheritance and interfaces.



Inheritance


Inheritance is an object-oriented programming technique that lets you use one class as the basis for another. The existing class is called the base class, superclass, or parent class; the new class that's derived from it is called the derived class, subclass, or child class.



When you create a subclass, the subclass is automatically given all the methods and fields defined by its superclass. You can use these methods and fields as-is, or you can override them to alter their behavior. In addition, you can add methods and fields that define data and behavior that's unique to the subclass.



You could use inheritance to solve the baseball/softball problem by creating a class named Ball that provides the basic features of all types of balls and then using it as the base class for separate classes named BaseBall and SoftBall. Then, these classes could override the methods that need to behave differently for each type of ball.



One way to think of inheritance is as a way to implement is-a-type-of relationships. For example, a softball is a type of ball, as is a baseball. Thus inheritance is an appropriate way to implement these related classes.



Interfaces


An interface is a named set of public method and field declarations. Note that the interface itself doesn't provide any code to implement those methods. Instead, it just provides the declarations. Then, a class that implements the interface provides code for each of the methods the interface defines.



You could use an interface to solve the baseball/softball problem by creating an interface named Ball that specifies all the methods and fields that a ball should have. Then, you could create the SoftBall and BaseBall classes so that they both implement the Ball interface.



Interfaces are closely related to inheritance, but have two key differences:



  • The interface itself doesn't provide code that implements any of its methods. An interface is just a set of method and field signatures. In contrast, a base class can provide the implementation for some or all of its methods.

  • A class can have only one base class. However, a class can implement as many interfaces as necessary.









dummies

Source:http://www.dummies.com/how-to/content/working-with-related-classes-in-java.html

No comments:

Post a Comment