Inheritance ITI1121 Nour El Kadri.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
1 More About Derived Classes and Inheritance Chapter 9.
Inheritance.
Modern Programming Tools And Techniques-I
Classes and Objects Introduced
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Lecture 12 Inheritance.
Final and Abstract Classes
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Chapter 11 Object-Oriented Design
Advanced Programming in Java
Week 4 Object-Oriented Programming (1): Inheritance
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 9 Inheritance and Polymorphism
Inheritance Chapter 5.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Object Oriented Programming
Java – Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Review of Previous Lesson
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
CS 200 More Classes Jim Williams, PhD.
Object-Oriented PHP (1)
Chapter 8 Inheritance Part 2.
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inheritance ITI1121 Nour El Kadri

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.

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.

OOP-Summary As a general principle, in ITI 1121, 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).

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.

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.

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

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.

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

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.

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

Classes Classes that are never instantiated are 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”.

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

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 as circles and rectangles. All shapes must have two instance variables, x and y, to represent the location of each object.

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

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

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.

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

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.

public final double getX() { } public final double getY() { return y; 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;

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

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:

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

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

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.

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;

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

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

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.

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.

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.

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

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

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.

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.

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

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;

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

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

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); Rectangle r = new Rectangle(); System.out.println (r); Rectangle s = new Rectangle (50, 50, 10, 15); System.out.println (s); s.flip(); }

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

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

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.