1 / 17

Inheritance and Class Hierarchies

Inheritance and Class Hierarchies. Ellen Walker CPSC 201 Data Structures Hiram College. Inheritance. Encapsulation of code into classes makes reuse easier But many times we want to reuse “most of” a class.

ahenninger
Download Presentation

Inheritance and Class Hierarchies

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College

  2. Inheritance • Encapsulation of code into classes makes reuse easier • But many times we want to reuse “most of” a class. • Inheritance allows information to be organized so that new classes can be derived from existing ones, instead of starting from scratch.

  3. COMPUTER LAPTOP SERVER DESKTOP IBook Vaio An Inheritance Hierarchy superclass • Each object inherits data and methods from parent • Goal: put information as close to root as possible subclass

  4. Inheritance Example • Computer • Hard drive • Processor • Laptop • Battery • (inherits Hard Drive, Processor from Computer) • Server • Additional processors • (inherits Hard Drive, Processor from Computer)

  5. Inheritance in Java • All Java classes are arranged in a hierarchy • Extensive (e.g. Throwable sub-hierarchy) • Your objects will fit somewhere in this hierarchy • Object is the superclass of all Java classes • Subclasses refine or extend their superclasses • A laptop is a computer • A student is a person • A TA is a student

  6. Is-a Versus Has-a Relationships • IS-A • Denotes subclass relationship • E.g. Laptop IS-A computer • In Java, “extends” • HAS-A • Denotes component relationship • E.g. Laptop HAS-A battery • In Java, one class is a data member of another class

  7. Superclass and Subclass in UML • Triangle-arrow denotes a subclass relationship • The superclass is also called “base class”

  8. Three Kinds of Object Visibility • Public - accessible to any other class • Use for methods at the “interface” • Private - accessible to no other class • Use for data members, as well as methods to be called only by the methods of this class • Protected - accessible to subclasses (and classes in the same package) • Use when subclasses will need to access item, but external classes should not • Dangerous if package contains unrelated classes (or no package defined)

  9. Constructing a Subclass • When inherited objects are private, the subclass cannot access them to initialize! • This is normal and correct • Use super() to explicitly construct the superclass first (with appropriate parameters) • If not, super() (with no parameters) is implicitly called • If the superclass doesn’t have a default (no-parameter) constructor, this causes a compiler error.

  10. Overriding Objects • Parameter or local object overrides a data object in the class with the same name; use this. to specify the “hidden” object public void setName( String name ) { this.name = name; } • Subclass’s method overrides parent’s method; use super. to specify the “hidden” method public String toString () { super.toString() + “ “ + battery; } • Overridden methods must have the same return type

  11. Method Overloading • Method overloading: having multiple methods with the same name but different signatures (number and type of parameters) in a class • Constructors are often overloaded • Example: • MyClass(int inputA, int inputB) • MyClass(int inputA, int inputB, double inputC)

  12. Polymorphism • Polymorphism means many forms or many shapes • In OOP, polymorphism refers to a single (superclass) object that can belong to one of many (subclass) classes • Example: list of shapes • Shape[] myShapes = new Shape[5]; • myShapes[0] = new Square(5); • myShapes[1] = new Circle(2);

  13. Which Method to Run? • Consider: for (int j=0;j<2;j++){ System.out.println(Shapes[j].area()) } • Compiler doesn’t know which area() method to run! • Polymorphism allows the JVM to determine which method to invoke at run time • Even though it’s different for different iterations of the loop!

  14. Abstract Classes • An interface declares abstract methods • Contain name and parameters but no implementation • An abstract class is between an interface and a (concrete) class • Can contain abstract methods (specified with keyword “abstract”) • Can also contain concrete methods and data objects • Used for specifying polymorphic objects (e.g. Shape)

  15. Classes, Abstract Classes and Interfaces

  16. Abstract Classes and Interfaces • Like an interface, an abstract class can’t be instantiated • An abstract class can have constructors to initialize its data fields when a new subclass is created • Subclass uses super(…) to call the constructor • May implement an interface but it doesn’t have to define all of the methods declared in the interface • Implementation is left to its subclasses

More Related