Summary

This document discusses inheritance in object-oriented programming, using Java. It covers the concept of inheritance, superclasses and subclasses, the super keyword, and constructor chaining. It also provides examples of inheritance in action, showing how to create objects and use inherited methods.

Full Transcript

Inheritance#1 Object-oriented programming allows you to define new classes from existing classes. This is called inheritance In this lecture, the following topics are covered:  The inheritance concept  Super classes and sub classes  The super keyword  Constructor Chai...

Inheritance#1 Object-oriented programming allows you to define new classes from existing classes. This is called inheritance In this lecture, the following topics are covered:  The inheritance concept  Super classes and sub classes  The super keyword  Constructor Chaining 1. Introduction o Inheritance is an important and powerful feature for reusing software. o Suppose you need to define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so as to avoid redundancy and make the system easy to comprehend and easy to maintain? The answer is to use inheritance. 2. Super classes and sub classes o Inheritance enables you to define a general class (i.e., a superclass) and later extend it to more specialized classes (i.e., subclasses). o You use a class to model objects of the same type. Different classes may have some common properties and behaviors, which can be generalized in a class that can be shared by other classes. You can define a specialized class that extends the generalized class. The specialized classes inherit the properties and methods from the general class. o Consider geometric objects. Suppose you want to design the classes to model geometric objects such as circles and rectangles. 10 ‫ من‬1 ‫الصفحة‬ o Geometric objects have many common properties and behaviors. They can be drawn in a certain color and be filled or unfilled. Thus a general class GeometricObject can be used to model all geometric objects. o This class contains the properties color and filled and their appropriate getter and setter methods. o Since a circle is a special type of geometric object, it shares common properties and methods with other geometric objects. Thus it makes sense to define the Circle class that extends the GeometricObject class. Likewise, Rectangle can also be defined as a subclass of GeometricObject. Figure 1 shows the relationship among these classes. A triangular arrow pointing to the superclass is used to denote the inheritance relationship between the two classes involved. Figure 1. The GeometricObject class is the superclass for Circle and Rectangle. 10 ‫ من‬2 ‫الصفحة‬ o In Java terminology, a class C1 extended from another class C2 is called a subclass, and C2 is called a superclass. o A superclass is also referred to as a parent class or a base class, and a subclass as a child class, an extended class, or a derived class. o A subclass inherits accessible data fields and methods from its superclass and may also add new data fields and methods. o The Circle class inherits all accessible data fields and methods from the GeometricObject class. In addition, it has a new data field, radius, and its associated getter and setter methods. o The Circle class also contains the getArea(), getPerimeter(), and getDiameter() methods for returning the area, perimeter, and diameter of the circle. o The Rectangle class inherits all accessible data fields and methods from the GeometricObject class. In addition, it has the data fields width and height and their associated getter and setter methods. It also contains the getArea() and getPerimeter() methods for returning the area and perimeter of the rectangle. Listing1. The Circle class 10 ‫ من‬3 ‫الصفحة‬ Listing 2. The GeometricObject class. 10 ‫ من‬4 ‫الصفحة‬ o The Circle class (Listing 1) extends the GeometricObject class (Listing 2) using the following syntax:  The keyword extends tells the compiler that the Circle class extends the GeometricObject class, thus inheriting the methods getColor, setColor, isFilled, setFilled, and toString.  The overloaded constructor Circle (double radius, String color, boolean filled) is implemented by invoking the setColor and setFilled methods to set the color and filled properties. These two public methods are defined in the superclass GeometricObject and are inherited in Circle, so they can be used in the Circle class.  You might attempt to use the data fields color and filled directly in the constructor as follows: This is wrong, because the private data fields color and filled in the GeometricObject class cannot be accessed in any class other 10 ‫ من‬5 ‫الصفحة‬ than in the GeometricObject class itself. The only way to read and modify color and filled is through their getter and setter methods. The code in Listing 3 shows how to create an object of the circle class and use the methods inherited from the GeometricObject class. Listing3. Testing the inheritance relationship between the Circle and GeometricObject class. The output Note the following points regarding inheritance: o A subclass is not a subset of its superclass. In fact, a subclass usually contains more information and methods than its superclass. o Private data fields in a superclass are not accessible outside the class. Therefore, they cannot be used directly in a subclass. They can, however, be accessed/mutated through public accessors (getters) if defined in the superclass. 10 ‫ من‬6 ‫الصفحة‬ o Inheritance is used to model the is-a relationship. Do not blindly extend a class just for the sake of reusing methods. For example, it makes no sense for a Tree class to extend a Person class, even though they share common properties such as height and weight. A subclass and its superclass must have the is-a relationship. o Not all is-a relationships should be modeled using inheritance. For example, a square is a rectangle, but you should not extend a Square class from a Rectangle class, because the width and height properties are not appropriate for a square. Instead, you should define a Square class to extend the GeometricObject class and define the side property for the side of a square. o Some programming languages allow you to derive a subclass from several classes. This capability is known as multiple inheritance. o Java, however, does not allow multiple inheritance. o A Java class may inherit directly from only one superclass. This restriction is known as single inheritance. If you use the extends keyword to define a subclass, it allows only one parent class. o In Java, multiple inheritance can be achieved through interfaces. 3. The super keyword o The keyword super refers to the superclass and can be used to invoke the superclass’s methods and constructors. o A subclass inherits accessible data fields and methods from its superclass. Does it inherit constructors? Can the superclass’s constructors be invoked from a subclass? o The keyword super refers to the superclass of the class in which super appears. It can be used in two ways:  To call a superclass constructor 10 ‫ من‬7 ‫الصفحة‬  To call a superclass method 3.1 Calling Superclass Constructors o A constructor is used to construct an instance of a class. o Unlike properties and methods, the constructors of a superclass are not inherited by a subclass. They can only be invoked from the constructors of the subclasses using the keyword super. o The syntax to call a superclass’s constructor is: o The statement super() invokes the no-arg constructor of its superclass, and the statement super(arguments) invokes the superclass constructor that matches the arguments. o The statement super() or super(arguments) must be the first statement of the subclass’s constructor; this is the only way to explicitly invoke a superclass constructor. For example, the constructor in Listing 2 can be replaced by the following code: 3.2 Constructor Chaining o A constructor may invoke an overloaded constructor or its superclass constructor. If neither is invoked explicitly, the compiler automatically puts super () as the first statement in the constructor. For example: 10 ‫ من‬8 ‫الصفحة‬ o In any case, constructing an instance of a class invokes the constructors of all the superclasses along the inheritance chain. o When constructing an object of a subclass, the subclass constructor first invokes its superclass constructor before performing its own tasks. o If the superclass is derived from another class, the superclass constructor invokes its parent-class constructor before performing its own tasks. o This process continues until the last constructor along the inheritance hierarchy is called. This is called constructor chaining. Consider the following code: 10 ‫ من‬9 ‫الصفحة‬ The output: Since no constructor is explicitly defined in Apple, Apple’s default no-arg constructor is defined implicitly. Since Apple is a subclass of Fruit, Apple’s default constructor automatically invokes Fruit’s no-arg constructor. However, Fruit does not have a no-arg constructor, because Fruit has an explicit constructor defined. Therefore, the program cannot be compiled. 10 ‫ من‬10 ‫الصفحة‬

Use Quizgecko on...
Browser
Browser