COM 244 Object Oriented Programming Some parts of
COM 244 Object Oriented Programming Some parts of those slides are adapted on course materials by Stuart Reges http: //www. cs. washington. edu/331/
Inheritance • Inheritance: Forming new classes using existing ones. • a way to reuse code between more than one classes • superclass: is the name given to the class that is being inherited from • subclass: is the class that inherited from superclass (parent class). • gets a copy of every field and method of superclass • is-a relationship: is called specialization. Each object of the subclass also "is a(n)" object of the superclass and can be treated as one. 2
Inheritance 3
Inheritance syntax and definition public class name extends superclass { • Example: public class Mammals extends Animal {. . . } • By extending Animal, each Mammals object now: • obtains a copy of each method of Animal class automatically • can be considered as an Animal by user code • Animal can also override behavior from Mammal 4
Overriding Methods • override: is to write a new version of a method in a subclass that is inherited from superclass • In java, there is no special syntax required to override a method of a superclass • For instance public class Lion extends Mammals { // overrides Praying in Mammals class public void Pray() {. . . } } 5
The super keyword • A subclass can call its parent's method/constructor: super. method(parameters) super(parameters); // method // constructor public class Lion extends Mammals { public Lion(String name) { super(name); } } 6
Protected fields/methods protected type name; // field protected type name(type name, . . . , type name) { statement(s); // method } • a protected field or method can be called either the class itself, and/or its subclasses public class Lion { protected double speed; . . . } 7
Inheritance Example 8
Circle Definition 9
Shapes using Inheritance 10
Shapes using Inheritance • In the earlier Point and Circle examples, it would be better to have a Color class and to generalize the concept of a Shape with a Shape class. • What is shared to a Circle and Square? • Both have an origin and color. • Both need a way to calculate area and perimeter • It will not make sense to have an instance of Shape class. Thus it should be an abstract class …. 11
Shapes using Inheritance public abstract class Shape { public abstract double area(); private Color col; public abstract double perimeter(); private Point org; protected Shape (Color col, Point org) public Color get. Color() { { return col; this. org = new Point (org. get. X(), } org. get. Y()); public void set. Color(Color col) { this. col = new Color (col. get. R(), col. set. RGB(col. get. R(), col. get. G(), col. get. B()); } } protected Shape (Color col, Point org) { public Point get. Origin() { ori = new Point (org. get. X(), return org; org. get. Y()); } this. col = new Color (0, 0, 0); public void set. Origin(Point org) { } this. org. set. Point(org. get. X(), protected Shape () { org. get. Y()); this. org = new Point (0, 0); this. col = new Color (0, 0, 0); } } } 12
Inheritance – Circle Class public double area() { public class Circle extends Shape { return Math. PI * rad; } private double rad; public double get. Radius() { public Circle() { return rad; super(); } rad = 0; } public double perimeter() { public Circle(final Point org, return 2 * (3. 14) * rad; final double rad) { } super(org); this. rad = rad; public void set. Radius(double r) { } this. rad = r; } } 13
Inheritance – Square Class public class Square extends Shape { public double perimeter() { return 4 * edge; private double edge; } public Square() { public double get. E() { super(); return edge; edge = 0. 0; } } public Square(Point org, double e) public void set. E(double e) { { super(org); edge = e; } } } public double area() { return edge * edge; } 2012 14
A Case Study 2012 15
A Case Study 16
A Case Study 17
A Case Study 18
A Case Study 19
- Slides: 19