Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College."— 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 An Inheritance Hierarchy COMPUTER LAPTOP DESKTOP SERVER IBook Vaio Each object inherits data and methods from parent Goal: put information as close to root as possible superclass 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

7 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

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

9 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)

10 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.

11 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

12 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)

13 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);

14 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!

15 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)

16 Classes, Abstract Classes and Interfaces

17 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


Download ppt "Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College."

Similar presentations


Ads by Google