Docsity

Java Inheritance, Slides of Computer Science

Partial preview of the text

Download Java Inheritance and more Slides Computer Science in PDF only on Docsity!

Java Inheritance

Inheritance

  • (^) On the surface, inheritance is a code re-use issue. - (^) we can extend code that is already written in a manageable manner.
  • (^) Inheritance is more
    • (^) it supports polymorphism at the language level

Inheritance Example

  • (^) Employee: name, email, phone
    • (^) FulltimeEmployee: also has salary, office, benefits, …
      • (^) Manager: CompanyCar, can change salaries, rates contracts, offices, etc.
    • (^) Contractor: HourlyRate, ContractDuration, …
  • (^) A manager is a special kind of FullTimeEmployee, which is a special kind of Employee.

Polymorphism

  • (^) Create code that deals with general object types, without the need to know what specific type each object is.
  • (^) Generate a list of employee names:
    • (^) all objects derived from Employee have a name field since Employee has a name field
    • (^) no need to treat managers differently from anyone else.

Shapes

  • (^) Shape:
    • (^) color, layer fields
    • (^) draw() draw itself on the screen
    • (^) calcArea() calculates it's own area.
    • (^) serialize() generate a string that can be saved and later used to re-generate the object.

Kinds of Shapes

  • (^) Rectangle
  • (^) Triangle
  • (^) Circle Each could be a kind of shape (could be specializations of the shape class). Each knows how to draw itself, etc. Could write code to have all shapes draw themselves, or save the whole collection to a file.

Abstract Class modifier

  • (^) Abstract modifier means that the class can be used as a superclass only. - (^) no objects of this class can be created. - (^) can have attributes, even code - (^) all are inherited - (^) methods can be overridden
  • (^) Used in inheritance hierarchies

Interesting Method Modifiers

  • (^) private/protected/public :
    • (^) protected means private to all but subclasses
    • (^) what if none of these specified?
  • (^) abstract : no implementation given, must be supplied by subclass. - (^) the class itself must also be declared abstract
  • (^) final : the method cannot be changed by a subclass (no alternative implementation can be provided by a subclass).

Inheritance vs. Composition

  • (^) When one object type depends on another, the relationship could be: - (^) is-a - (^) has-a
  • (^) Sometimes it's hard to define the relationship, but in general you use composition (aggregation) when the relationship is has-a

Composition

  • (^) One class has instance variables that refer to object of another.
  • (^) Sometimes we have a collection of objects, the class just provides the glue. - (^) establishes the relationship between objects.
  • (^) There is nothing special happening here (as far as the compiler is concerned).

Inheritance

  • (^) A derived class object is an object of the base class. - (^) is-a, not has-a. - (^) all fields and methods are inherited.
  • (^) The derived class object also has some stuff that the base class does not provide (usually).

Java Inheritance

  • (^) Two kinds:
    • (^) implementation: the code that defines methods.
    • (^) interface: the method prototypes only.
  • (^) Other OOP languages often provide the same capabilities (but not as an explicit option).

Accessing superclass methods

from derived class.

  • (^) Can use super() to access all (non-private) superclass methods. - (^) even those replaced with new versions in the derived class.
  • (^) Can use super() to call base class constructor. - (^) use arguments to specify desired constructor

Single inheritance only

(implementation inheritance).

  • (^) You can't extend more than one class!
    • (^) the derived class can't have more than one base class.
  • (^) You can do multiple inheritance with interface inheritance.