PEMROGRAMAN BERORIENTASI OBJEK
Java Method Overriding
In this tutorial, we will learn about method overriding in Java with the help
of examples.
In the last tutorial, we learned about inheritance. Inheritance is an OOP
property that allows us to derive a new class (subclass) from an existing
class (superclass). The subclass inherits the attributes and methods of the
superclass.
Now, if the same method is defined in both the superclass and the
subclass, then the method of the subclass class overrides the method of
the superclass. This is known as method overriding.
Example 1: Method Overriding
class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
@Override
public void displayInfo() {
System.out.println("I am a dog.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Output:
I am a dog.
In the above program, the displayInfo() method is present in both
the Animal superclass and the Dog subclass.
When we call displayInfo() using the d1 object (object of the subclass), the
method inside the subclass Dog is called. The displayInfo() method of the
subclass overrides the same method of the superclass.
Notice the use of the @Override annotation in our example. In Java,
annotations are the metadata that we used to provide information to the
compiler. Here, the @Override annotation specifies the compiler that the
method after this annotation overrides the method of the superclass.
It is not mandatory to use @Override . However, when we use this, the method
should follow all the rules of overriding. Otherwise, the compiler will
generate an error.
Java Overriding Rules
Both the superclass and the subclass must have the same method
name, the same return type and the same parameter list.
We cannot override the method declared as final and static .
We should always override abstract methods of the superclass (will
be discussed in later tutorials).
super Keyword in Java Overriding
A common question that arises while performing overriding in Java is:
Can we access the method of the superclass after overriding?
Well, the answer is Yes. To access the method of the superclass from the
subclass, we use the super keyword.
Example 2: Use of super Keyword
class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
public void displayInfo() {
super.displayInfo();
System.out.println("I am a dog.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Output:
I am an animal.
I am a dog.
In the above example, the subclass Dog overrides the
method displayInfo() of the superclass Animal .
When we call the method displayInfo() using the d1 object of
the Dog subclass, the method inside the Dog subclass is called; the method
inside the superclass is not called.
Inside displayInfo() of the Dog subclass, we have used super.displayInfo() to
call displayInfo() of the superclass.
It is important to note that constructors in Java are not inherited. Hence,
there is no such thing as constructor overriding in Java.
However, we can call the constructor of the superclass from its subclasses.
For that, we use super() . To learn more, visit Java super keyword.
Access Specifiers in Method Overriding
The same method declared in the superclass and its subclasses can have
different access specifiers. However, there is a restriction.
We can only use those access specifiers in subclasses that provide larger
access than the access specifier of the superclass. For example,
Suppose, a method myClass() in the superclass is declared protected . Then,
the same method myClass() in the subclass can be either public or protected ,
but not private .
Example 3: Access Specifier in Overriding
class Animal {
protected void displayInfo() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
public void displayInfo() {
System.out.println("I am a dog.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Output:
I am a dog.
In the above example, the subclass Dog overrides the
method displayInfo() of the superclass Animal .
Whenever we call displayInfo() using the d1 (object of the subclass), the
method inside the subclass is called.
Notice that, the displayInfo() is declared protected in the Animal superclass.
The same method has the public access specifier in the Dog subclass. This
is possible because the public provides larger access than the protected .
Overriding Abstract Methods
In Java, abstract classes are created to be the superclass of other classes.
And, if a class contains an abstract method, it is mandatory to override it.
We will learn more about abstract classes and overriding of abstract
methods in later tutorials.
Java super
In this tutorial, we will learn about the super keyword in Java with the help
of examples.
The super keyword in Java is used in subclasses to access superclass
members (attributes, constructors and methods).
Before we learn about the super keyword, make sure to know about Java
inheritance.
Uses of super keyword
1. To call methods of the superclass that is overridden in the subclass.
2. To access attributes (fields) of the superclass if both superclass and
subclass have attributes with the same name.
3. To explicitly call superclass no-arg (default) or parameterized
constructor from the subclass constructor.
Let’s understand each of these uses.
1. Access Overridden Methods of the superclass
If methods with the same name are defined in both superclass and
subclass, the method in the subclass overrides the method in the
superclass. This is called method overriding.
Example 1: Method overriding
class Animal {
// overridden method
public void display(){
System.out.println("I am an animal");
}
}
class Dog extends Animal {
// overriding method
@Override
public void display(){
System.out.println("I am a dog");
}
public void printMessage(){
display();
}
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.printMessage();
}
}
Output
I am a dog
In this example, by making an object dog1 of Dog class, we can call its
method printMessage() which then executes the display() statement.
Since display() is defined in both the classes, the method of
subclass Dog overrides the method of superclass Animal . Hence,
the display() of the subclass is called.
What if the overridden method of the superclass has to be called?
We use super.display() if the overridden method display() of
superclass Animal needs to be called.
Example 2: super to Call Superclass Method
class Animal {
// overridden method
public void display(){
System.out.println("I am an animal");
}
}
class Dog extends Animal {
// overriding method
@Override
public void display(){
System.out.println("I am a dog");
}
public void printMessage(){
// this calls overriding method
display();
// this calls overridden method
super.display();
}
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.printMessage();
}
}
Output
I am a dog
I am an animal
Here, how the above program works.
2. Access Attributes of the Superclass
The superclass and subclass can have attributes with the same name. We
use the super keyword to access the attribute of the superclass.
Example 3: Access superclass attribute
class Animal {
protected String type="animal";
}
class Dog extends Animal {
public String type="mammal";
public void printType() {
System.out.println("I am a " + type);
System.out.println("I am an " + super.type);
}
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.printType();
}
}
Output:
I am a mammal
I am an animal
In this example, we have defined the same instance field type in both the
superclass Animal and the subclass Dog .
We then created an object dog1 of the Dog class. Then,
the printType() method is called using this object.
Inside the printType() function,
type refers to the attribute of the subclass Dog .
super.type refers to the attribute of the superclass Animal.
Hence, System.out.println("I am a " + type); prints I am a mammal .
And, System.out.println("I am an " + super.type); prints I am an animal .
3. Use of super() to access superclass constructor
As we know, when an object of a class is created, its default constructor is
automatically called.
To explicitly call the superclass constructor from the subclass constructor,
we use super() . It's a special form of the super keyword.
super() can be used only inside the subclass constructor and must be the
first statement.
Example 4: Use of super()
class Animal {
// default or no-arg constructor of class Animal
Animal() {
System.out.println("I am an animal");
}
}
class Dog extends Animal {
// default or no-arg constructor of class Dog
Dog() {
// calling default constructor of the superclass
super();
System.out.println("I am a dog");
}
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
}
}
Output
I am an animal
I am a dog
Here, when an object dog1 of Dog class is created, it automatically calls the
default or no-arg constructor of that class.
Inside the subclass constructor, the super() statement calls the constructor
of the superclass and executes the statements inside it. Hence, we get the
output I am an animal .
The flow of the program then returns back to the subclass constructor and
executes the remaining statements. Thus, I am a dog will be printed.
However, using super() is not compulsory. Even if super() is not used in the
subclass constructor, the compiler implicitly calls the default constructor of
the superclass.
So, why use redundant code if the compiler automatically invokes
super()?
It is required if the parameterized constructor (a constructor that takes
arguments) of the superclass has to be called from the subclass
constructor.
The parameterized super() must always be the first statement in the body of
the constructor of the subclass, otherwise, we get a compilation error.
Example 5: Call Parameterized Constructor Using super()
class Animal {
// default or no-arg constructor
Animal() {
System.out.println("I am an animal");
}
// parameterized constructor
Animal(String type) {
System.out.println("Type: "+type);
}
}
class Dog extends Animal {
// default constructor
Dog() {
// calling parameterized constructor of the superclass
super("Animal");
System.out.println("I am a dog");
}
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
}
}
Output
Type: Animal
I am a dog
The compiler can automatically call the no-arg constructor. However, it
cannot call parameterized constructors.
If a parameterized constructor has to be called, we need to explicitly define
it in the subclass constructor.
Note that in the above example, we explicitly called the parameterized
constructor super("Animal") . The compiler does not call the default
constructor of the superclass in this case.
SUMBER: PROGRAMIZ.COM