Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems."— Presentation transcript:

1 Inheritance CSI 1101 Nour El Kadri

2 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.

3 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.

4 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).

5 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.

6 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.

7 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 {... }

8  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.

9 Examples Thus, the following two declarations are therefore identical: class C {... } and class C extends Object {... }

10 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.

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

12 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”.

13 Abstract Classes What are abstract classes use for then?  An abstract class defines the characteristics that are common to all its subclasses.

14 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.

15 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

16 Shape Methods – Cont’d  void scale(double factor); // scales the shape by some factor  String toString(); // returns a String representation of an object

17  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.

18 Class Declaration public abstract class Shape extends Object { // Instance variables private double x; private double y; // Constructors public Shape() { x = 0; y = 0; }

19 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.

20 public final double getX() { return x; } public final double getY() { return y; } // Common instance method public final void moveTo (double x, double y) { this.x = x; this.y = y; }

21 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); }

22 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:

23 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:0 new Time (0,59,60) -> 1:0:0 new Time (23,59,60) -> 0:0:0

24

25 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().

26 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.

27 public class Circle extends Shape { private double radius; // Instance variable public Circle() {// Constructors super(); radius = 0; } public Circle(double x, double y, double radius) { super(x, y); this.radius = radius; } public double getRadius() { // Access method return radius; }

28 // 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(); }

29 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); }

30

31 Using a method defined in the superclass  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.

32 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.

33 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.

34 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; }

35 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;... }

36  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.

37  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.

38  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; }

39 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; }

40 public double area() { return width * height; } public void scale(double factor) { width = width * factor; height = height * factor; }

41 public void flip() { double tmp = width; width = height; height = tmp; } public String toString() { return "Rectangle: + super.toString() + "Width: " + width + "Height: " + height + "Area: " + area(); }

42 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); }

43 Circle: Located at: (0.0,0.0) Radius: 0.0 Area: 0.0 Circle: Located at: (100.0,200.0) Radius: 10.0 Area: 9.934588265796101 Circle: Located at: (100.0,200.0) Radius: 20.0 Area: 14.049629462081453

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

45 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.


Download ppt "Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems."

Similar presentations


Ads by Google