OOP Inheritance Inheritance A class can extend another

  • Slides: 17
Download presentation
OOP: Inheritance

OOP: Inheritance

Inheritance • A class can extend another class, inheriting all its data members and

Inheritance • A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. • Inheritance represents the is a relationship, an object of a subclass also can be treated as an object of its superclass. • A has-a relationship represents composition. In a has-a relationship, a class object contains references to objects of other classes.

Superclasses and Subclasses

Superclasses and Subclasses

Example // Cylinder. java: Class definition for describing Cylinder public class Cylinder extends Circle

Example // Cylinder. java: Class definition for describing Cylinder public class Cylinder extends Circle { private double length = 1; /** Return length */ public double get. Length() { return length; } /** Set length */ public void set. Length(double length) { this. length = length; UML Diagram } /** Return the volume of this cylinder */ public double find. Volume() { return find. Area() * length; } }

Example (cont. ) Cylinder cylinder = new Cylinder(); System. out. println("The length is "

Example (cont. ) Cylinder cylinder = new Cylinder(); System. out. println("The length is " + cylinder. get. Length()); System. out. println("The radius is " + cylinder. get. Radius()); System. out. println("The volume of the cylinder is " + cylinder. find. Volume()); System. out. println("The area of the circle is " + cylinder. find. Area()); The output is: The length is 1. 0 The radius is 1. 0 The volume of the cylinder is 3. 14159 The area of the circle is 3. 14159

Creating a Subclass Creating a subclass extends properties and methods from the superclass. You

Creating a Subclass Creating a subclass extends properties and methods from the superclass. You can also: • Add new properties • Add new methods • Override the methods of the superclass 6

Inheritance Hierarchy • A class may have several subclasses and each subclass may have

Inheritance Hierarchy • A class may have several subclasses and each subclass may have subclasses of its own. • The collection of all subclasses descended from a common ancestor is called an inheritance hierarchy.

Constructors in Subclasses • The general rule is that when a subclass is created

Constructors in Subclasses • The general rule is that when a subclass is created Java will call the superclass constructor first and then call the subclass constructors in the order determined by the inheritance hierarchy. • If a superclass does not have a default constructor with no arguments, the subclass must explicitly call the superclass constructor with the appropriate arguments.

Using the Keyword super • The keyword super refers to the superclass of the

Using the Keyword super • The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways: – To call a superclass constructor – To call a superclass method

CAUTION • You must use the keyword super to call the superclass constructor. •

CAUTION • You must use the keyword super to call the superclass constructor. • Invoking a superclass constructor’s name in a subclass causes a syntax error. • Java requires that the statement that uses the keyword super appear first in the constructor.

NOTE • A constructor is used to construct an instance of a class. Unlike

NOTE • A constructor is used to construct an instance of a class. Unlike properties and methods, a superclass's constructors are not inherited in the subclass. • They can only be invoked from the subclasses' constructors, using the keyword super. • If the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked.

Overriding Methods in the Superclass A subclass inherits methods from a superclass. Sometimes it

Overriding Methods in the Superclass A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overriding. // Cylinder. java: New cylinder class that overrides the find. Area() // method defined in the circle class. public class Cylinder extends Circle { /** Return the surface area of this cylinder. The formula is * 2 * circle area + cylinder body area */ public double find. Area() { return 2 * super. find. Area() + 2 * get. Radius() * Math. PI * length; } // Other methods are omitted } 12

Method Overriding • If a derived class has a method found within its base

Method Overriding • If a derived class has a method found within its base class, that method will override the base class’s method. • The keyword super can be used to gain access to superclass methods overridden by the base class. • A subclass method must have the same return type as the corresponding superclass method.

Visibility Modifiers 14

Visibility Modifiers 14

Visibility Modifiers • Private data fields belonging to a base class must be initialized

Visibility Modifiers • Private data fields belonging to a base class must be initialized by invoking the base class’s constructor with the appropriate parameters • If the execution of any constructor in a subclass does not invoke a superclass constructor, Java automatically invokes the no-parameter constructor for the superclass – Initializes that part of the object inherited from the superclass before the subclass starts to initialize its part of the object.

Casting Objects • • Mountain. Bike my. Bike = new Mountain. Bike(); then my.

Casting Objects • • Mountain. Bike my. Bike = new Mountain. Bike(); then my. Bike is of type Mountain. Bike is descended from Bicycle and Object. Therefore, a Mountain. Bike is a Bicycle and is also an Object, and it can be used wherever Bicycle or Object objects are called for. The reverse is not necessarily true: a Bicycle may be a Mountain. Bike, but it isn't necessarily. Similarly, an Object may be a Bicycle or a Mountain. Bike, but it isn't necessarily. Casting shows the use of an object of one type in place of another type, among the objects permitted by inheritance and implementations. For example, if we write Object obj = new Mountain. Bike(); then obj is both an Object and a Mountainbike (until such time as obj is assigned another object that is not a Mountainbike). This is called implicit casting. If, on the other hand, we write Mountain. Bike my. Bike = obj; we would get a compiletime error because obj is not known to the compiler to be a Mountain. Bike. However, we can tell the compiler that we promise to assign a Mountain. Bike to obj by explicit casting: Mountain. Bike my. Bike = (Mountain. Bike)obj; This cast inserts a runtime check that obj is assigned a Mountain. Bike so that the compiler can safely assume that obj is a Mountain. Bike. If obj is not a Mountainbike at runtime, an exception will be thrown. Note: You can make a logical test as to the type of a particular object using the instanceof operator. This can save you from a runtime error owing to an improper cast. For example: if (obj instanceof Mountain. Bike) { Mountain. Bike my. Bike = (Mountain. Bike)obj; } Here the instanceof operator verifies that obj refers to a Mountain. Bike so that we can make the cast with knowledge that there will be no runtime exception thrown.

Thank You

Thank You