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

Advanced Object-Oriented Programming Concepts Lecture 17 Administrative Issues • Due Today – Program 3 at 11pm • Assigned Today – Program 4 • Due June 20 th , one week from today Advanced OOP • Focus on concepts in this lecture • Worry less about syntax, more about ideas Inheritance Class Definitions • Define classes “from scratch” • Start with empty file • Add parts one at a time – Add class name – Variables – Methods • The class defines a basic template for an object Vehicle class defines a basic motorized vehicle Related Classes • Sometimes, we don’t need to start from scratch • Suppose we wanted a Car class – It’s a lot like the Vehicle class, but more specialized – Both classes have wheels, a motor, a speed – They can accelerate and slow down • Can we somehow reuse the Vehicle class – The Answer: Inheritance

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

View Full Document
Inheritance • When defining a new class, we can derive its definition from previously defined classes – Start with the old class definition – Add new variables and/or methods as needed • This process is called Inheritance Classes as Classifications • The term class comes from the term classification – All objects of a given class are similar in nature • Classification schemes can have many levels – Example: Shape Rectangle Square Inheritance • Using Inheritance –A Shape class would be defined – Derive a Rectangle class from the Shape class – Derive a Square class from the Rectangle class Shape Rectangle Square Why Inheritance? • Why not define each independently? – Faster and easier to derive classes Software Reuse • Take advantage of code that has already been created and tested Shape Rectangle Square Terminology • A parent class – Base class – Superclass –Examp l e s Shape is the superclass of Rectangle Rectangle is the parent class of Square • A child class – Subclass –Examp l e s Rectangle is a subclass of Shape Square is a child class of Rectangle Shape Rectangle Square The extends Reserved Word • We say a child class extends a base class • Example: public class
Inheritance: An is-a” Relationship • Java uses the word extends • We can think of it as an is-a relationship Car is a Vehicle Novel is a Book Square is a Rectangle is a Shape CivicDX is a Civic is a Honda is a Sedan is a Car is a Vehicle Example: Shape Class • An arbitrary shape has many properties • ALL SHAPES have three of those properties – Center position • X and Y position –A rea (x 1 ,y 1 ) (x 2 ,y 2 ) Example: Shape Class public class Shape { protected float area=1.0, centerX=0.0, centerY=0.0; public Shape( float area, float centerX, float centerY) { this .area = area; this.centerX = centerX; this.centerY = centerY; } public void printMessage() { System.out.println(“Shape with area ” + area + “at (” + centerX + “,” + centerY + “)”; } } Extending Shape to Circle

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}