5.1 Derived classes

Derived class (subclass): a class that is derived from another class, called a base class (superclass)
Inheritance (is-a): derived class inherts the properties of base class
extends: declare derived class after the derived class name followed by the base class name

public class ProduceItem extends GenericItem {...}

Variations:

  • Derived class can serve as a base class for another class
  • A class can serve as the base class for multiple derived classes

5.2 Is-a versus has-a relationship

Composition (has-a): one object may be made up of other objects
Unified Modeling Language (UML)
Slid line with a closed, unfilled arrowhead: indicates inheritance

5.3 UML (optional)

5.4 Access by members of derived classes

Members of a derived class have access to the public members of the base class, but no to the private members of the base class
Protected: provides access to derived classes and all classes in the same package but not by anyone else

SpecifierDescription
privateAccessible by self
protectedAccessible by self, derived classes, and other classes in the same package
publicAccessible by self, derived classes, and everyone else
no specifier (package-private)Accessible by self and other classes in the same package

Encapsulation: restricting access to data from outside a class; a goal of object-oriented programming (OOP)
Protected access modifier loosens encapsulation

In class definition, public and no specifier specifies a class’s visibility in other classes

5.5 Overriding member methods

Override: a derived class defines a member method that has the same name and parameters as a base class’s method
@Override: placed above the method that overrides so the compiler verifies that an identical base class method exists
Annotation: optional command beginning with the @ symbol that provide the compiler with information to detect errors

NOTE: an overriding method needs to have the same method signature and a covariant return type (the same return type or a subtype of the original return type)

Overloading: methods have the same name but different parameter types or number of parameters
super: a reference variable used to call the parent class’s methods or constructors

5.6 The Object class

Built-in Object class: serves as the base class for all other classes and has no base class; common methods defined include toString() and equals(otherObject)
toString(): returns a String representation of this object; by default, returns this object’s class name followed by the object’s hash code in hexidecimal form
equals(otherObject): method compares this object to other object; by default, returns true if both variables reference the same object

5.7 Polymorphism

Polymorphism: determining which program behavior to execute depending on data types
Compile-time polymorphism: compiler determins which of several identically-named methods to call based on the method’s arguments (Ex., method overloading)
Runtime polymorphism: compiler cannot make the determination but instead the determination is made while the program is running

A reference to a derived class can be converted to a reference to the base class (without explicit casting)

NOTE: a base-class reference can refer to a derived-class object, but it can only directly access methods and fields available in the base class; overridden methods execute the derived class’s version at runtime

5.8 ArrayLists of Objects

NOTE:

// performs autoboxing and upcasting
Object num = 1;
ArrayList<Object> list = new ArrayList<Object>();
list.add(new Object());
list.add(new String("Hello, World!"));
list.add(true);
list.add(2);
// since all classes inherit from Object,
// all classes have toString() method, making this legal
for (int i = 0; i < list.size(); i++) System.out.println(list.get(i).toString());

5.9 Abstract classes: Introduction (generic)

Classes: a class encapsulates data and behavior to create objects
Inheritance: inheritance allows one class (a subclass) to be based on another class (a base class or superclass)
Abtract classes: a class that guides the design of subclasses but cannot itself be instantiated as an object

NOTE: a concrete subclass must implement all abstract methods inherited from its superclass (an abstract class can inherit abstract methods without implementing)
NOTE: use an abstract method when a method is common to all subclasses but its implementation differs

5.10 Abstract classes

Abstract method: a method not implemented in the base class that all concrete subclasses must override (abstract in front of method signature)
Method signature: defines the method’s name and parameters
Abstract class: a class that guides the design of subclasses but cannot itself be instantiated as an object (abstract in front of class definition); any class with any abstract methods must be abstract
Concrete class: a class that is not abstract and can be initialized

NOTE: an abstract class can be used as a reference type for upcasting


NOTE: instanceof checks the inheritance relationship, while getClass() returns the exact runtime class. Ex.,

class Animal {}
class Dog extends Animal {}
 
Animal a = new Dog();
 
System.out.println(a instanceof Animal); // true
System.out.println(a instanceof Dog); // true
System.out.println(a.getClass() == Animal.class); // false
System.out.println(a.getClass() == Dog.class); // true

Every object in Java inherits from Object
Inherits implementations from parent class. Change behavior or add implementation using overload and override

Constructors are not inherited
A fields with private will only be accessible in the defined class

@Override is an annotation

.equals(Object o) must override with parameter type Object

Java automatically inserts super() to call parent’s no-argument constructor
super() must be the first statement in the constructor
If parent class has no no argument constructor, must explicitly call a mathing super(args)
Java automatically creates a no argument constructor only if you define no constructors at all

Java uses the runtime type to choose which overriden method runs (except static and private)