Inheritance

Inheritance - Java Inheritance The "IS-A" way to...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Java Inheritance The "IS-A" way to get code reuse (The other way is "HAS-A".) The Person and Customer classes share an "IS-A" relation ________ | | | Person | |________| /\ / \ /____\ || __________________________||_______________________________ | | | | | ____|_____ ____|___ ___|___ ____|_____ ____|____ | | | | | | | | | | | Customer | | Expert | | Staff | | Developer| | Manager | |__________| |________| |_______| |__________| |_________| The Customer Object ________ / \ / \ | Object | \ / \________/ / \ / \ | Person | \ / \________/ / \ / \ | Customer | \ / \________/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
An object is made up of multiple classes. Even if the class didn't use the keyword extends to specify a parent class it gets Object as a parent. Java has "single inheritance" so only one class can be specified as a parent. Function implementation Common, general functions (like getName()) are in the parent class Person. Unique, specific functions (like getCustomerNumber()) are in the child class Customer. Function availability Person class methods can be called on the Customer object: Customer c = new Customer(); c.setName("Bubba"); Person can be specified a "parent only" class Adding the keyword abstract to the class declaration line of Person means it cannot be instantiated into an object. public abstract class Person { ... } This statement in another application would yeild a compiler error: X Person p = new Person();
Background image of page 2
The Object class has only a few methods But they are helpful! The toString() method in the Object class will print the class name. Thus you can always count on being able to print any object. In a "business object" (a
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Inheritance - Java Inheritance The "IS-A" way to...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online