Introduction to Inheritance Syntax

Share on:

In this post we'll cover very basic syntax for inheritance in Java. We'll use examples mentioned here. We'll also look at different UML diagrams and try to convert those diagrams to actual classes using Java syntax.

Design 1

Using inheritance for Code Reusability

To inherit one class from another, we use the extends keyword. Notice the declaration of Square class (and others as well)

 1class Shape {
 2
 3    public void rotate() {
 4
 5        System.out.println("Shape's rotate()");
 6    }
 7
 8    public void playSound() {
 9
10        System.out.println("Shape's playSound()");
11    }
12}
1class Square extends Shape {
2
3
4}
1class Circle extends Shape {
2
3
4}
1class Triangle extends Shape {
2
3
4}
1class Rectangle extends Shape {
2
3
4}
 1class HelperClass {
 2
 3    public static void main(String[] args) {
 4        
 5        Square square = new Square();
 6        square.rotate();
 7        square.playSound();
 8
 9        Circle circle = new Circle();
10        circle.rotate();
11        circle.playSound();
12
13        Triangle triangle = new Triangle();
14        triangle.rotate();
15        triangle.playSound();
16
17        Rectangle rectangle = new Rectangle();
18        rectangle.rotate();
19        rectangle.playSound();
20    }
21}

Output:

1Shape's rotate()
2Shape's playSound()
3Shape's rotate()
4Shape's playSound()
5Shape's rotate()
6Shape's playSound()
7Shape's rotate()
8Shape's playSound()

Notice how calling rotate() or playSound() on any class will call Shape class's implementation. This is the desired output as per our design.

Design 2

Overriding rotate() method

Now let's see how we'll use method overriding. We'll add a Rhombus class to our code.

1class Rhombus extends Shape {
2
3    public void rotate() {
4
5        System.out.println("Rhombus's rotate()");
6    }
7}

And we'll change our helper class to the following

 1class HelperClass {
 2
 3    public static void main(String[] args) {
 4        
 5        Square square = new Square();
 6        square.rotate();
 7        square.playSound();
 8
 9        Circle circle = new Circle();
10        circle.rotate();
11        circle.playSound();
12
13        Triangle triangle = new Triangle();
14        triangle.rotate();
15        triangle.playSound();
16
17        Rectangle rectangle = new Rectangle();
18        rectangle.rotate();
19        rectangle.playSound();
20
21        Rhombus rhombus = new Rhombus();
22        rhombus.rotate();
23        rhombus.playSound();
24    }
25}
 1Shape's rotate()
 2Shape's playSound()
 3Shape's rotate()
 4Shape's playSound()
 5Shape's rotate()
 6Shape's playSound()
 7Shape's rotate()
 8Shape's playSound()
 9Rhombus's rotate()
10Shape's playSound()

Notice the change in output, now calling rotate() function on Rhombus will not call Shape's version instead will call Rhombus's own version because we've overridden this method. Notice how calling playSound() on Rhombus still calls Shape's version of the method.

comments powered by Disqus