| Gettysburg College |
CS112 Intro to Computer Science II
Inheritance in Java
- Extended Object-Oriented Design
- hierarchical (tree-like) structure
- allows to derive new classes from existing ones
- recognize IS-A relationships in the problem solution
any descendent IS-A more specific version of its ancestor- put "common" properties and behaviors in a parent/super/base class.
- terms: paret-child, super-sub, base-derived
- approaches:
- generalization (bottom-up)
- specialization (top-down)
- Inheritance in Java
- can only inherit from a single class
- multiple inheritance (or a similar effect) can be achieved using Java interfaces
- parent class = superclass = base class
- child class = subclass = derived class
- ALL classes inherit from Object class, if not specified otherwise
- What gets inherited and how they are accessed
- Parent's constructors can be accessed in the Child's constructors (first statement)
- public/protected/default methods/variables/constants are directly accessible
- private variables/methods/constants are not directly accessible, but can be accessed via non-private methods
- New Java keywords
- class A extends B: class A inherits from class B
- super.xxx or super.yyy(..) or super(...)
- protected: new access modifier, available within the family hierarch and within the same package
- abstract
- Constructors in Java and Java Inheritance
- a default constructor (empty parameter list) is provided by Java IF there is NO constructor in the class
This allows creating an object with new ClassName().- if a class has a non-default constructor, Java does NOT provide a default constructor.
new ClassName() results in an error- class B extends A: new B() or new B(...) automatically executes new A() if not specified.
- class B extends A: to call a different parent constructor, use super(....) as the FIRST line in the child's constructor(s).
- Parent's constructor is automatically executed first when a Child object is created to ensure all inherited data members are initialized.
- Terminology
- overriding vs. overloading
- overloading: one method (same name and return type) with different signatures
- overriding: child class replaces ancestor's implementation of a non-static method (same signature)
- polymorphism: a single identifier (variable name) can represent multiple different (itself and descendents) types at different times of the program's execution.
Strawberry berry1 = new Strawberry(); Blueberry berry2 = new Blueberry(); Apple apple = new Apple(); Fruit fruit1 = new Orange(); // what kind of conversion is this? System.out.println(fruit1); ... fruit1 = berry1; // what kind of conversion is this? System.out.println(fruit1); ... fruit1 = berry2; // what kind of conversion is this? System.out.println(fruit1); ... fruit1 = apple; // what kind of conversion is this? System.out.println(fruit1);- conversion:
- widening conversion
- narrowing conversio
- dynamic binding: @ runtime, when a method is invoked, the method definition (implementation) is searched, first in the class of the object itself, then its parent, then its grand parent, ... up the family tree, until one is found and executed.
- Abstract Classes
- primary purpose: build more special purpose classes
- serves as a base class only
- cannot be instantiated (new AbstractClass(...) is NOT possible)
- can be created as a reference variable
- syntax: abstract class AbstractClassName { ... }
- at least one method is an abstract method: has a signature but no body at all (no curly braces!)
- abstract method(s) must be implemented by a derived (child) class to be instantiable
- Object class
- ancestor of ALL Java classes
- provides a common framework upon which to build derived classes
- any derived class has all of the methods in the Object class: toString(), equals(Object), hashCode(), ...
- allows to define special-purpose classes which can still be manipulated (accessed) as generic Objects
Object obj1 = new Integer(10); Object obj2 = new String("Hello World"); Object obj3 = new Card("Hearts", 9); Object obj4 = 4; (does NOT work)