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

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. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
Lecture 32 Inheritance COMP1681 / SE15 Introduction to Programming.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance using Java
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
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.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
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.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
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.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
 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.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Final and Abstract Classes
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Week 6 Object-Oriented Programming (2): Polymorphism
Object Oriented Programming
Chapter 11 Inheritance and Polymorphism
CS 200 More Classes Jim Williams, PhD.
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inheritance CSI 1101 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 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).

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

Abstract Classes What are abstract classes use 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 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() { 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); System.out.println(d); }

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

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

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: Rectangle: Located at: (50.0,50.0) Width: 15.0 Height: 10.0 Area: 150.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.