CS2_21_InheritanceObjectSuperThis

CS2_21_InheritanceObjectSuperThis - CS2 Module 21 Category:...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CS2 Module 21 Category: OO Concepts Topic: Inheritance Objectives Inheritance class Object check it out in the API! Scope: super/this CS 2 Introduction to Object Oriented Programming Module 21 OO Concepts Inheritance Inheritance Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types of personnel. Much of the code for different classifications of personnel would be identical This was often handled by cutting and pasting code and then modifying Needed a way to capture and formalize the similarity Inheritance Allows us to take advantage of a class that already have that is close to what we need. Later we will see it allows us to take advantage of polymorphism ! The new class we write is called a " subclass " By "extending" the pre-existing class (known as the " superclass "), we can add new data beyond which is present in the superclass add new methods beyond which is present in the superclass redefine any methods of the superclass that we desire. ( Method Overriding !) class Point { int x,y; // Fields public Point() { // Constructor 1 this(0,0); } public Point(int x, int y) { // Constructor 2 setX(x); setY(y); } public void setX(int x) { // Modifier for x this.x = x; } public int getX() { // Accessor for x return x; } // Assume setY and getY exist also // Note } // class Point Consider class PointChild extends Point { } public class Driver { public static void main(String args) { PointChild pc = new PointChild(); pc.setX(42); System.out.println(pc.getX()+" "+pc.getY()); } } Legal? What does it do? Consider class LabelledPoint extends Point { String name; public LabelledPoint(int x, int y, String name) { setName(name); this(x, y); } public void setName(int name) { this.name = name; } public String getName() { return name; } } Notice We have now created a new class without having to rewrite about 50% of the required code! How useful is this??? Very!!! LabelledPoint parent/superclass Point child/subclass Natural, hierarchical way of organizing things. Staff Member Employee Volunteer Hourly Salaried Consultant Think in terms of "is a" relationships: An Employee is a Staff Member, as is a Volunteer. An Hourly worker is a(n) Employee. A Consultant is a(n) Hourly employee. (subclass of Hourly) (subclass of Employee) (subclass of Staff) (superclass) Inheritance class Animal { public String name = ""; public String noise = ""; public int numTimesPerformed = 0; // constructors, accessors & modifiers go here public void identifySelf( ) { System.out.println("My name is " + name); } // of identifySelf public void perform( ) { doYourThing( ); numTimesPerformed++; } // of perform public void doYourThing( ) { System.out.println("Doin' my thing!"); } // of doYourThing } // of Animal Don't worry about "private" and "public" for now So, animals have a name and noise and they can identify themselves, perform...
View Full Document

Page1 / 50

CS2_21_InheritanceObjectSuperThis - CS2 Module 21 Category:...

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

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