0% found this document useful (0 votes)
38 views

Tutorial 9

Uploaded by

Opel Frontera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Tutorial 9

Uploaded by

Opel Frontera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

(/cn/)

(/en/) Welcome (/en/Welcome) / Inheritance

Previous Tutorial (/en/Compiling_and_Running_with_Arguments)

Next Tutorial (/en/Try_and_Catch)

Inheritance
Inheritence in Java allows you to reuse code from an existing class into another class, you can derive your new
class from an existing class. Your new class is called derived class which inherits all the members from its
superclass.

The inherited fields can be used directly, just like any other fields. You can declare a field in the subclass with the
same name as the one in the superclass, thus hiding it (not recommended). You can declare new fields in the
subclass that are not in the superclass. The inherited methods can be used directly as they are. You can write a
new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
You can write a new static method in the subclass that has the same signature as the one in the superclass, thus
hiding it. You can declare new methods in the subclass that are not in the superclass. You can write a subclass
constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super. A
subclass does not inherit the private members of its parent class.

An example of inheritence
Consider a class called Shape, Shape is the base class which is inherited by shapes like rectangle, square, circle
etc.

public class Shape {


public double area ()
{
return 0; // Since this is just a generic "Shape" we will assume the area as zero.
// The actual area of a shape must be overridden by a subclass, as we see below.
// You will learn later that there is a way to force a subclass to override a metho
d,
// but for this simple example we will ignore that.
}
}

Execute Code

Class Shapeis inherited by Circle which is a Shape.


The method area is defined in the base class and has been inherited in the circle class and
areamethod
is
available with the circle class which is redefined specific to circle.

class Circle extends Shape { // The "extends" keyword is what we use to tell java tha
t Circle inherits the functionality of Shape.

private static final double PI = Math.PI; // constant


private double diameter; // This could be any number, representing the diameter o
f this circle.

public double area () {


double radius = diameter / 2.0;
return PI * radius * radius;
}
Code Window Run Reset Solution
}
Output Window Expected Output Show Code Window

Execute Code

(http://www.dmca.com/Protection/Status.aspx?ID=fd56e7e29e1f43ccbe7ce1023cb5781c)
The advantage of using inheritance is that you can write code that can apply to a number of classes that
extendCopyright © LearnJavaOnline.org. Read our Terms of Use (/tos) and Privacy Policy (/privacy) a more
general class. In the below example we have a method that takes the larger area from the two shapes:

As you can see,

public class Main {


public static void main(String[] args) {
Shape s1 = new Circle (5.0);
Shape s2 = new Rectangle (5.0, 4.0);
Shape larger = getLargerShape(s1,s2);

System.out.println("The area of the larger shape is: "+larger.area());


}

public static Shape getLargerShape(Shape s1, Shape s2) {


if(s1.area() > s2.area())
return s1;
else
return s2;
}
}

Execute Code

getLargerShape() doesn't require the programmer to input a specific type of shape for its 2 parameters. You could
use an instance of any class that inherits the type Shape as any of the two parameters for this method. You could
use an instance of type Circle , Rectangle , Triangle , Trapezoid , etc. as long as they extend Shape .
Exercise
Create a rectangle class which inherits the Shape class and finds the area

Start Exercise

(http://www.spoj.com/?utm_campaign=permanent&utm_medium=banner&utm_source=learnx)

Previous Tutorial (/en/Compiling_and_Running_with_Arguments)

Next Tutorial (/en/Try_and_Catch)

Code Window Run Reset Solution

Output Window Expected Output Show Code Window

(http://www.dmca.com/Protection/Status.aspx?ID=fd56e7e29e1f43ccbe7ce1023cb5781c)
Copyright © LearnJavaOnline.org. Read our Terms of Use (/tos) and Privacy Policy (/privacy)

You might also like