Chapter 8 Summary
- A Java interface contains the headings of public methods and the definitions of public named constants. It does not declare constructors or private instance variables.
- A class that implements an interface must define a body for every method that the interface specifies. It might also define methods not declared in the interface. A class can implement more than one interface.
- An interface provides a way for a class designer to specify methods for another programmer. By implementing an interface, a programmer can guarantee that a class defines certain methods.
- An interface is a reference type, and so you can declare variables and method parameters as having an interface type.
- You can extend an interface to create an interface that consists of the methods in the existing interface plus some new methods.
- Dynamic, or late, binding is a process that enables objects of different classes to substitute for one another, if they have identical interfaces. This ability—called polymorphism—allows different objects to use different method actions for the same method name.
- A derived class is obtained from a base class by adding instance variables and methods. The derived class inherits all public instance variables and public methods that are in the base class.
- When defining a constructor for a derived class, you definition should first call a constructor of the base class by using
super
. If you do not make an explicit call, Java will automatically call the default constructor of the base class.
- Within a constructor,
this
calls a constructor of the same class, but super invokes a constructor of the base class.
- You can redefine a method from a base class so that it has a different definition in the derived class. This is called overriding the method definition.
- When you override a method definition, the new method definition given in the derived class must have the same name, the exact same number and types of parameters, and the same return type as the method in the base class. If the method in the derived class has a different number of parameters or a parameter position of a different type from the method in the base class, the method is overloaded, not overridden.
- Within the definition of a method of a derived class, you can call an overridden method of the base class by prefacing the method name with super and a dot.
- Private instance variables and private methods of a base class cannot be accessed directly by name within a derived class.
- An object of a derived class has the type of the derived class, and it also has the type of the base class. More generally, a derived class has the type of every one of its ancestor classes.
- You can assign an object of a derived class to a variable of any ancestor type, but not the other way around.
- In Java, every class is a descendant of the predefined class
Object
. So every object of every class is of type Object
, as well as the type of its class and any other ancestor classes.