Skip to content

Latest commit

 

History

History

ch01_classesobjects

1. Classes and objects

Inheritance

  • provide storing of common attributes and behavior in parent class
  • child class can (depends on access privilege of attribute or method):
    • use attributes and methods from parent
    • overshadow attribute (with same name) from parent
    • expand or override methods from parent class
  • it is not possible to assign instance of parent class into variable of type the child class
    • child class (used as variable reference type) may contains attributes or methods which are not defined in parent class (used as variable object type)
    • similar situation is with class that implements some interfaces

Constructors

  • see OCA, chapter 5 Constructors
  • child constructor always call (implicitly or explicitly) parent constructor first
    • explicit use super() or super(param) in case of parameterized parent constructor

Methods

  • override method in child class:
    • can access to first parent method implementation using super.method();
    • can't add other checked exception to throws clause
  • throws clause of override method in child class can:
    • add unchecked exception
    • use same exception as parent method
    • use subclass of exception used in parent method
    • hide checked exception (omit throws clause)

Immutable class

Rules of immutability

  • make all fields final and private
  • don't provide "setter" methods
  • don't allow subclasses to override methods (declare the class as final or make the constructor private and construct instances in factory methods)
  • if the instance fields include references to mutable objects, don't allow those objects to be changed:
    • don't provide methods that modify the mutable objects.
    • don't share references to the mutable objects
      • if necessary, create copies and return references to the copies)
    • never store references to external, mutable objects passed to the constructor
      • if necessary, create copies, and store references to the copies)

Instanceof

  • determines if object is instance of class or interface
  • if the reference type of checked object is unreachable with checked class, the compilation error is caused
    • it's not relevant if reference type is interface (compilation error is not caused in this case)