CS2_21_InheritanceObjectSuperThis

CS2_21_InheritanceObjectSuperThis - CS2 Module 21 Category...

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

View Full Document Right Arrow Icon
CS2 Module 21 Category: OO Concepts Topic: Inheritance Objectives Inheritance class Object – check it out in the API! Scope: super/this
Image of page 1

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

View Full Document Right Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 21 OO Concepts Inheritance
Image of page 2
Inheritance
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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 !)
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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?
Image of page 7

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

View Full Document Right Arrow Icon
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; } }
Image of page 8
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
Image of page 9

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

View Full Document Right Arrow Icon
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
Image of page 10
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 and do their thing.
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern