inheritance csi 1101 nour el kadri. oop we have seen that object-oriented programming (oop) helps...

45
Inheritance CSI 1101 Nour El Kadri

Upload: jemima-norton

Post on 29-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Inheritance

CSI 1101Nour El Kadri

Page 2: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

OOP

We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.

The data, and the methods that act upon the data, are encapsulated into a single entity called the object.

The instance variables define the properties or state of an object.

In particular, we have seen that OOP provides mechanisms to control the visibility of the methods and the variables.

Page 3: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

OOP-Summary

The methods and variables that are public define the interface of the object.

It’s useful to distinguish between the creator of the class (the programmer who creates the class) and the client of the class (the programmer who will be using the class).

Having the interface clearly defined allows the creator and the client to work independently; the creator can change the implementation of the class, as long as it does not affect the interface, and the programs developed by the client will continue to work.

Page 4: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

OOP-Summary

As a general principle, in CSI 1101, all the instance variables should be declared private.

If the value of a variable needs to be accessed (read or mutated) from outside the class, then the interface of the object will include setter and getter methods.

This principle will allow us to maintain the integrity of the objects.

The class specifies the content of the objects but it also exists during the execution of a program. Each object knows the class from which it was instantiated from (is an instance of).

Page 5: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

OOP-Summary

No matter how many instances there are, 0, 1 or n, there is only one copy of the class.

Class variables and methods are shared by all instances of a class.

In today’s lecture, we look at other important features of object-oriented programming that help organizing and maintaining large software systems: inheritance and polymorphism.

Page 6: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Inheritance

OO languages, in general, also offer other tools to structure large systems. Inheritance is one of them.

Inheritance allows to organize the classes hierarchically.

The class immediately above is called the superclass or parent class while the class immediately below is called the subclass, child class or derived class.

Page 7: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Inheritance - Example

In this example, Bird is the superclass of Pigeon, i.e. Pigeon “is a” subclass of Bird.

In Java, this “is a” relationship is expressed as follows: class Pigeon extends Bird {

...

}

Page 8: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

In Java, the classes are organized into a single hierarchy, with the most general class, called Object, being at the top (or root) of the tree.

If the superclass is not explicitly mentioned, Object is the immediate parent class.

Page 9: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Examples

Thus, the following two declarations are therefore identical: class C {

...}

and class C extends Object {

...}

Page 10: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Class vs. Superclass A class inherits all the characteristics

(variables and methods) of its superclass(es).

1. a subclass inherits all the methods and variables of its superclass(es);

2. a subclass can introduce/add new methods and variables;

3. a subclass can override the methods of its superclass.

Page 11: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Because of 2 and 3, the subclass is a specialization of the superclass, i.e. the superclass is more general than its subclasses.

Page 12: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Classes

Classes that are never instantiated as abstract.If a class is declared abstract, as follows:

public abstract class C extends Object {...}

then the following,

C o = new C();

will produce a compile time error saying “C is abstract; cannot be instantiated”.

Page 13: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Abstract Classes

What are abstract classes use for then?

An abstract class defines the characteristics that are common to all its subclasses.

Page 14: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Shape - Example

This example can be found in most textbooks about object-oriented programming.

A software system must be developed to represent various shapes, such circles and rectangles.

All shapes must have two instance variables, x and y, to represent the location of each object.

Page 15: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Shape Methods

Furthermore, we would like the shapes to have the following methods: double getX(); // returns the value of x double getY(); // returns the value of y void moveTo(double x, double y);

// move the shape to a new location double area(); // calculates the area of

the shape

Page 16: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Shape Methods – Cont’d

void scale(double factor);

// scales the shape by some factor String toString();

// returns a String representation of an object

Page 17: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

The implementation of the first three methods would be the same for all kinds of shapes.

On the other hand, the calculation of the area and the implementation of the scale method would depend on the kind of shape being dealt with.

Finally, the method toString() requires information from both levels, general and specific, all shapes should display their location and also their specific information, such as the radius in the case of a circle.

Page 18: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Class Declarationpublic abstract class Shape extends Object {

// Instance variables

private double x;

private double y;

// Constructors

public Shape() {

x = 0;

y = 0;

}

Page 19: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public Shape (double x, double y) {

this.x = x;

this.y = y;

}

// Access instance methods: the methods are

// declared final so that it is not possible

// to override them in a subclass.

Page 20: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public final double getX() {return x;

}

public final double getY() {return y;

}

// Common instance methodpublic final void moveTo (double x, double y) {

this.x = x;this.y = y;

}

Page 21: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public String toString() {return "Located at: (" + x + "," + y + ")";

}

// The following instance methods cannot be implemented// here, because calculating the area of circle differs from// that of a square. However, we require that all shapes have// an area() method. Similarly all shapes must be scalable.

abstract public double area();abstract public void scale (double factor);

}

Page 22: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Solution

The constructor is a special method which is called (executed) when an object is first created, this is therefore the ideal place to put the necessary mechanisms that will ensure that the initial values are in the correct range:

Let’s create a new method which will do the job of normalizing the values of the instance variables:

Page 23: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public Time(int hours, int minutes, int seconds) {this.seconds = seconds;this.minutes = minutes;this.hours = hours;normalise();

}new Time (0,0,60) -> 0:1:0new Time (0,59,60) -> 1:0:0new Time (23,59,60) -> 0:0:0

Page 24: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems
Page 25: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public class Circle extends Shape {

}

The above declaration defines a class Circle that extends Shape. Which means that an instance of the class Circle will possess two instance variables x and y, plus the following methods: area(), scale(double factor), getX(), getY(), moveTo(double x, double y) and toString().

Page 26: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Problem

However, the above definition will not compile! Why?

Because, the superclass Shape does not provide an implementation for the two abstract methods area() and scale(double factor); an instance of Circle would not be functional.

The class Circle must also define the attributes and methods that are specific to a Circle.

Page 27: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public class Circle extends Shape {private double radius; // Instance variable

public Circle() { // Constructorssuper();radius = 0;

}

public Circle(double x, double y, double radius) {super(x, y);this.radius = radius;

}

public double getRadius() { // Access methodreturn radius;

}

Page 28: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

// Implementation of the abstract methods

public double area() {

return Math.PI * radius * radius;

}

public void scale(double factor) {

radius *= factor;

}

public String toString() {

return "Circle:"

+ super.toString()

+ "Radius: " + radius

+ "Area: " + area();

}

Page 29: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public static void main (String args[]) {Circle c = new Circle();System.out.println(c);Circle d = new Circle(10, 100, 5);System.out.println(d);d.scale(2);System.out.println(d);}}

Page 30: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems
Page 31: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Using a method defined in thesuperclass

Since a class inherits all the methods defined in the superclass, it would be possible to use moveTo() within Circle.

However, the definition of toString() overrides the one defined in the superclass.

The notation super.toString() allows to call explicitly the method defined in the superclass.

Page 32: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Constructors and Inheritance

The constructors are not inherited by a subclass!

The constructors of the subclass Circle use the constructors defined in the parent class, super(), then initializes the variable that was defined in the subclass,

this.radius = radius.

Page 33: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Looking up for a method

If an instance method is called, the JVM will first look up in the object, if the method cannot be found, Java will look up for a method with that name in the superclass, this process continues all the way up in the hierarchy. (follow the arrows)

The method System.out.println(Object o) calls the method toString() of its argument, o, if the method toString was not redefined in the object’s class nor in its superclass then the generic method toString, which is defined in the class Object is called.

Page 34: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Private vs. protected

With the current definition of the class Shape, it would not have been possible to define the constructor of the class Circle as follows:

public Circle(double x, double y, double radius) {

this.x = x;

this.y = y;

this.radius = radius;

}

Page 35: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Private vs. protected – Cont’d

The compiler would complain saying “x has private access in Shape” (and similarly for y).

This is because an attribute declared private in the parent class cannot be accessed within the child class.

To circumvent this and implement the constructor as above, the definition of Shape should be modified so that x and y would be declared protected:

public abstract class Shape extends Object {protected double x;protected double y;...}

Page 36: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

When possible, it is preferable to maintain the visibility private.

Private instance variables and final instance methods go hand in hand.

The declaration of an instance variable private prevents the subclasses from accessing the variable.

The declaration of a method final prevents subclasses from overriding the method.

Page 37: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

By declaring the instance variables private and the access/mutator instance methods final, you ensure that all the modifications to the instance variables are “concentrated” in the class where they were first declared.

Page 38: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Classes can also be declared final which would prevent any extension of the class.

public class Rectangle extends Shape {

private double width;

private double height;

public Rectangle() {

super();

width = 0;

height = 0;

}

Page 39: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public Rectangle(double x, double y, double width, double height) {

super(x, y);this.width = width;this.height = height;

}public double getWidth() {

return width;}public double getHeight() {

return height;}

Page 40: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public double area() {

return width * height;

}

public void scale(double factor) {

width = width * factor;

height = height * factor;

}

Page 41: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public void flip() {double tmp = width;width = height;height = tmp;

}

public String toString() {return "Rectangle:

+ super.toString()+ "Width: " + width+ "Height: " + height+ "Area: " + area();

}}

Page 42: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

public class Test {public static void main (String args[]) {

Circle c = new Circle();System.out.println (c);

Circle d = new Circle (100, 200, 10);System.out.println (d);

d.scale (2);System.out.println (d);

Rectangle r = new Rectangle();System.out.println (r);

Rectangle s = new Rectangle (50, 50, 10, 15);System.out.println (s);

s.flip();System.out.println (s);

}}

Page 43: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Circle:Located at: (0.0,0.0)Radius: 0.0Area: 0.0

Circle:Located at: (100.0,200.0)Radius: 10.0Area: 9.934588265796101

Circle:Located at: (100.0,200.0)Radius: 20.0Area: 14.049629462081453

Page 44: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Rectangle:Located at: (0.0,0.0)Width: 0.0Height: 0.0Area: 0.0

Rectangle:Located at: (50.0,50.0)Width: 10.0Height: 15.0Area: 150.0

Rectangle:Located at: (50.0,50.0)Width: 15.0Height: 10.0Area: 150.0

Page 45: Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems

Summary

Inheritance allows to reuse code. The methods getX(), getY() and moveTo() were only defined in the class Shape.

Fixing a bug or making an improvement in the superclass will fix or improve all the subclasses.