CP213 Lesson 7: Object-Oriented Programming Concepts (PDF)
Document Details
Uploaded by Deleted User
Tags
Summary
This document explains inheritance, a key concept in object-oriented programming. It provides examples using Java programming. The concept of derived and base classes is discussed and illustrated, emphasizing code reuse.
Full Transcript
Inheritance is one of the main techniques in object-oriented programming (OOP). Using this technique, a very general form of a class is first defined and compiled, and then more specialized versions of the class are defined by adding instance variables and methods. The specialized classes are said t...
Inheritance is one of the main techniques in object-oriented programming (OOP). Using this technique, a very general form of a class is first defined and compiled, and then more specialized versions of the class are defined by adding instance variables and methods. The specialized classes are said to inherit the methods and instance variables of the general class. More specifically, inheritance is the process by which a new class is created from another class. The new class is called a derived class. The original class is called the base class. A derived class automatically has all the instance variables and methods that the base class has, and it can have additional methods and/or instance variables as well. Inheritance is especially advantageous because it allows code to be reused without having to copy it into the definition of the derived classes. In this lesson, we will describe how to develop programs in Java using the inheritance, which enables you to develop applications with less code, leading to cost-effective application development and fast application delivery. The Java programming language supports inheritance, that is classes can be derived or sub-classed from other classes, inheriting instance variables, methods, and nested classes from their superclasses. Except for Object class, which has no superclass in Java, every class has one and only one direct superclass. This is because Java supports only single inheritance, different from, for example, C++, which supports multiple inheritances. If no superclass is explicitly declared for a class, then the class becomes a subclass of the Object class. The most general class in Java is the Object class, it is the topmost class in the Java class hierarchy. Classes near the bottom of the hierarchy are specialized classes. They are created to provide more specific behaviour. A class can be derived from classes that are subclasses of other classes, which in turn are subclasses of other classes and so on, and ultimately derived from the topmost class in the Java class hierarchy, the Object class. Such a class is said to be a child of all the classes in the inheritance chain, stretching back to the Object class. 2.1 Derived Classes When designing certain classes, there is often a natural hierarchy for grouping them. For example, creating software for an administrative system for the employees of a company, there will be hourly employees and salaried employees. Hourly employees can be divided into full time and part-time workers. Salaried employees can be divided into technical staff and executive staff. All these employees share certain characteristics in common. All have a name and a hire date. The methods for setting and changing names and hire dates would be the same for all employees. Some employees have specialized characteristics, for example, hourly employees are paid an hourly wage, while salaried employees are paid a fixed wage. The methods for calculating wages for these two different groups would be different. You can define a class called Employee that includes all employees. This class can then be used to define classes for hourly employees and salaried employees. In turn, the HourlyEmployee class can be used to define a PartTimeHourlyEmployee class, and so forth. Since an hourly employee is an employee, it is defined as a derived class of the class Employee. A derived class is defined by adding instance variables and methods to an existing class. The existing class that the derived class is built upon is called the base class. The phrase extends BaseClass must be added to the derived class definition: public class HourlyEmployee extends Employee { … } When a derived class is defined, it is said to inherit the instance variables and methods of the base class that it extends. Class Employee defines the instance variables name and hireDate in its class definition. Class HourlyEmployee also has these instance variables, but they are not specified in its class definition. Class HourlyEmployee has additional instance variables wageRate and hours that are specified in its class definition. The relationship between the mentioned classes is depicted in Figure 1. The class HourlyEmployee inherits all of the Employee methods just as it inherits the instance variables of the class Employee. The class HourlyEmployee inherits the methods getName, getHireDate, setName, and setHireDate from the class Employee. Any object of the class HourlyEmployee can invoke one of these methods, just like any of its methods. A derived class, also called a subclass, is defined by starting with another already defined class, called a base class or superclass, and adding (and/or changing) methods, instance variables, and static variables. The derived class inherits all the public methods, all the public and private instance variables and all the public and private static variables from the base class. The derived class can add more instance variables, static variables, and/or methods. 2.2 Inherited Members A derived class automatically has all the instance variables, all the static variables, and all the public methods of the base class. Members from the base class are said to be inherited. Definitions for the inherited variables and methods do not appear in the derived class. The code is reused without having to explicitly copy it unless the creator of the derived class redefines one or more of the base class methods. 2.3 Parent and Child Classes A base class is often called the parent class, and a derived class is then called a child class. These relationships are often extended such that a class that is a parent of a parent, etc. of another class is called an ancestor class. If class A is an ancestor of class B, then class B can be called a descendent of class A. 2.4 Overriding a Method Definition Although a derived class inherits methods from the base class, it can change or override an inherited method if necessary. Derived classes override inherited methods to provide their version of the method. To override a method definition, a new method definition is simply placed in the class definition, just like any other method added to the derived class. In the examples below, the Square class overrides the area method that it inherits from the Shape class. package lesson07; public class Shape { public double area(double... dimentions) { double area = 0.0; if (dimentions.length == 2) { area = (dimentions * dimentions); }else { // other cases } return area; } } package lesson07; public class Square extends Shape { @Override public double area(double... dimentions) { double area = 0.0; area = (dimentions * dimentions); return area; } }