Hello World

[펌]Java Inheritance example 본문

Java/Core

[펌]Java Inheritance example

EnterKey 2016. 1. 10. 12:28
반응형

In this tutorial we will discuss about the inheritance in Java. The most fundamental element of Java is the class. A class represents an entity and also, defines and implements its functionality. In Java, classes can bederived from other classes, in order to create more complex relationships.

A class that is derived from another class is called subclass and inherits all fields and methods of its superclass. In Java, only single inheritance is allowed and thus, every class can have at most one direct superclass. A class can be derived from another class that is derived from another class and so on. Finally, we must mention that each class in Java is implicitly a subclass of the Object class.

Suppose we have declared and implemented a class A. In order to declare a class B that is derived from A, Java offers theextend keyword that is used as shown below:

1class A {
2    //Members and methods declarations.
3}
4 
5class extends A {
6    //Members and methods from A are inherited.
7    //Members and methods declarations of B.
8}

Java supports only public inheritance and thus, all fields and methods of the superclass are inherited and can be used by the subclass. The only exception are the private members of the superclass that cannot be accessed directly from the subclass. Also, constructors are not members, thus they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. In order to call the constructor of the superclass Java provides the keyword super, as shown below:

01class A {
02    public A() {
03        System.out.println("New A");
04    }
05}
06 
07class extends A {
08    public B() {
09        super();
10        System.out.println("New B");
11    }
12}

A sample example to present the inheritance in Java is shown below:

Animal.java:

01public class Animal {
02    public Animal() {
03        System.out.println("A new animal has been created!");
04    }
05     
06    public void sleep() {
07        System.out.println("An animal sleeps...");
08    }
09     
10    public void eat() {
11        System.out.println("An animal eats...");
12    }
13}

Bird.java:

01public class Bird extends Animal {
02    public Bird() {
03        super();
04        System.out.println("A new bird has been created!");
05    }
06     
07    @Override
08    public void sleep() {
09        System.out.println("A bird sleeps...");
10    }
11     
12    @Override
13    public void eat() {
14        System.out.println("A bird eats...");
15    }
16}

Dog.java:

01public class Dog extends Animal {
02    public Dog() {
03        super();
04        System.out.println("A new dog has been created!");
05    }
06     
07    @Override
08    public void sleep() {
09        System.out.println("A dog sleeps...");
10    }
11     
12    @Override
13    public void eat() {
14        System.out.println("A dog eats...");
15    }
16}

MainClass.java:

01public class MainClass {
02    public static void main(String[] args) {
03        Animal animal = new Animal();
04        Bird bird = new Bird();
05        Dog dog = new Dog();
06         
07        System.out.println();
08         
09        animal.sleep();
10        animal.eat();
11         
12        bird.sleep();
13        bird.eat();
14         
15        dog.sleep();
16        dog.eat();
17    }
18}

In this example we created three distinct classes, AnimalDog and Bird. Both Dog and Bird classes extend the Animalclass and thus, they inherit its members and methods. Moreover, as we can see below, each class overrides the methods ofAnimal and thus, both the Dog and Bird classes redefine the functionality of Animal’s methods.

A sample execution is shown below:

A new animal has been created!
A new animal has been created!
A new bird has been created!
A new animal has been created!
A new dog has been created!

An animal sleeps...
An animal eats...
A bird sleeps...
A bird eats...
A dog sleeps...
A dog eats...

A nested class has access to all the private members of its enclosing class, both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.

As already mentioned, a subclass inherits all of the public and protected members of its superclass. If the subclass is in the same package as its superclass, it also inherits the package-private members of the superclass. The inheritance in Java provides the following features:

  • You can declare a field in the subclass with the same name as the one in the superclass thus, hiding it. This is calledshadowing.
  • You can declare new fields in the subclass that are not in the superclass.
  • 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 declare new methods in the subclass that are not in the superclass.

Final abstract classes can exist in a hierarchy of types. For more information about abstract classes and how are used in Java, please refer to the Java abstract tutorial here.

Inheritance and Casting

When a class B extends a class A, then an instance of the B class is of type B, but also of type A. Thus, such an instance can be used in all cases where a class B or class A object is required. However, the reverse is not true! An instance of the class A is of course of type A, but it is not of type B.

Thus, we can use casting between the instances of classes. The cast inserts a runtime check, in order for the compiler to safely assume that the cast is used properly and is correct. If not, a runtime exception will be thrown.

A simple example that demonstrates the usage of casting is shown below:

1Animal a1 = new Dog();
2Animal a2 = new Bird();
3         
4a1.eat();
5a2.sleep();
6         
7// The following statements are incorrect.
8// Dog d = new Animal();
9// Bird b = new Animal();

A sample execution is shown below:

A dog eats...
A bird sleeps...

The instanceof operator

The instanceof operator can be used, in order to determine if an object is a valid instance of a specific type. It can be used to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface. A simple example is shown below:

1Dog d = new Dog();
2if(d instanceof Animal) {
3    Animal a = (Animal) d;
4    a.sleep();
5}
6d.sleep();

Interfaces

An interface in Java is an abstract type that is used to declare and specify a set of public methods and members. An interface can be implemented by a class. In this case, the class must provide an implementation for every method defined in the interface. A significant advantage of using interfaces is the fact that in Java, multiple interfaces can be implemented by a single class.

A sample example that uses both classes and multiple interfaces is shown below:

BasketballTeam.java:

1public interface BasketballTeam {
2    public void printBasketballName();
3}

FootballTeam.java:

1public interface FootballTeam {
2    public void printFootballName();
3}

Team.java:

01public class Team implements BasketballTeam, FootballTeam {
02 
03    private String name = null;
04     
05    public Team(String name) {
06        this.name = name;
07    }
08 
09    @Override
10    public void printFootballName() {
11        System.out.println("Football Team: \"" + name + " F.C.\"");
12    }
13 
14    @Override
15    public void printBasketballName() {
16        System.out.println("Basketball Team: \"" + name + " B.C.\"");
17    }
18     
19    public static void main(String[] args) {
20        Team t = new Team("Team A");
21        t.printBasketballName();
22        t.printFootballName();
23    }
24}

A sample execution is shown below:

Basketball Team: "Team A B.C."
Football Team: "Team A F.C."

Download the Eclipse Project

The Eclipse project of this example: InheritanceExample.zip.

This was a tutorial about the inheritance in Java.

반응형
Comments