OOP: Inheritance

Download Report

Transcript OOP: Inheritance

OOP: Inheritance
By: Lamiaa Said
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
Superclass
Circle
Circle Methods
Circle Data
Inheritance
Subclass
Cylinder
Circle Methods
Cylinder Methods
Circle Data
Cylinder Data
Example
// Cylinder.java: Class definition for describing Cylinder
public class Cylinder extends Circle {
private double length = 1;
/** Return length */
public double getLength() {
return length;
}
Superclass
Subclass
Circle
Cylinder
-radius
-length
+getRadius
+setRadius
+findArea
/** Set length */
public void setLength(double length) {
this.length = length;
}
/** Return the volume of this cylinder */
public double findVolume() {
return findArea() * length;
}
}
+getLength
+setLength
+findVolume
UML Diagram
Example (cont.)
Cylinder cylinder = new
System.out.println("The
cylinder.getLength());
System.out.println("The
cylinder.getRadius());
System.out.println("The
cylinder.findVolume());
System.out.println("The
cylinder.findArea());
Cylinder();
length is " +
radius is " +
volume of the cylinder is " +
area of the circle is " +
The output is:
The
The
The
The
length is 1.0
radius is 1.0
volume of the cylinder is 3.14159
area of the circle is 3.14159
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 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
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 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.
• 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 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 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 findArea()
// 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 findArea() {
return 2 * super.findArea() + 2 * getRadius() * Math.PI * length;
}
// Other methods are omitted
}
12
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.
Method Overloading
• Method overloading: having multiple methods
with the same name but different signatures in a
class.
• Constructors are often overloaded.
Polymorphism
• Polymorphism means many forms or many
shapes.
• Polymorphism allows the JVM to determine
which method to invoke at run time.
• At compile time, the Java compiler can’t
determine what type of object a superclass may
reference but it is known at run time.
Visibility Modifiers
package p1;
public class C1 {
public int x;
protected int y;
int z;
private int u;
public class C2 {
C1 o = new C1();
can access o.x;
can access o.y;
can access o.z;
cannot access o.u;
protected void m() {
}
}
can invoke o.m();
}
package p2;
public class C3
extends C1 {
can access x;
can access y;
can access z;
cannot access u;
public class C4
extends C1 {
can access x;
can access y;
cannot access z;
cannot access u;
can invoke m();
}
public class C5 {
C1 o = new C1();
can access o.x;
cannot access o.y;
cannot access o.z;
cannot access o.u;
can invoke m();
}
cannot invoke o.m();
}
16
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.
Thank You