Java Reference
In-Depth Information
Chapter Summary
Inheritance is a feature of Java programs that allows the
creation of a parent-child relationship between two types.
may be a poor design choice and a “has-a” relationship
between them (in which one object contains the other as a
field) may be better.
The child class of an inheritance relationship (commonly
called a subclass) will receive a copy of (“inherit”) every
field and method from the parent class (superclass). The sub-
class “extends” the superclass, because it can add new fields
and methods to the ones it inherits from the superclass.
An interface is a list of method declarations. An interface
specifies method names, parameters, and return types but
does not include the bodies of the methods. A class can
implement (i.e., promise to implement all of the methods
of) an interface.
A subclass can override a method from the superclass by
writing its own version, which will replace the one that
was inherited.
Interfaces help us achieve polymorphism so that we can
treat several different classes in the same way. If two or
more classes both implement the same interface, we can
use either of them interchangeably and can call any of the
interface's methods on them.
Treating objects of different types interchangeably is
called polymorphism.
An abstract class cannot be instantiated. No objects of the
abstract type can be constructed. An abstract class is use-
ful because it can be used as a superclass and can also
define abstract behavior for its subclasses to implement.
Subclasses can refer to the superclass's constructors or
methods using the super keyword.
The Object class represents the common superclass of all
objects and contains behavior that every object should
have, such as the equals and toString methods.
An abstract class can contain abstract methods, which are
declared but do not have bodies. All subclasses of an
abstract class must implement the abstract superclass's
abstract methods.
Inheritance provides an “is-a” relationship between two
classes. If the two classes are not closely related, inheritance
Self-Check Problems
Section 9.1: Inheritance Basics
1. What is code reuse? How does inheritance help achieve code reuse?
2. What is the difference between overloading and overriding a method?
3. Consider the following classes:
public class Vehicle {...}
public class Car extends Vehicle {...}
public class SUV extends Car {...}
 
 
Search WWH ::




Custom Search