- Inheritance.
- Method overriding and variables hiding.
- this and super keywords.
Inheritance
Inheritance is a process where one class can inherit visible properties and methods from another class — the parent-child relationship between two classes (or superclass and subclass).
// in Person.java file
public class Person {
public String name;
public String address;
public int age;
public void walk() {
System.out.println(name + " is walking.");
}
}// in Student.java file
public class Student extends Person {
public static void main(String[] args){
Student student = new Student();
student.name = "John Doe";
student.address = "101 Main St";
student.age = 22;
student.walk();
}
}
In the above example, the Student class extends the Person class so our Student class is a child class of Person class. Student class will extend all visible (depends on access modifiers of variables and methods) variables and methods.
Inheritance is useful for code reusability for example we can have one generic class that will have common properties and methods with default behaviors and the child classes can just extend it and reuse a lot of code. If the child class wants to have its own implementation for methods defined in the parent class, we can always override these methods in the child class.
One good example is java.lang.Object class. Object class is the parent class for all classes in java. Java automatically will inject extends Object syntax after every class declaration. Why every class needs to extend from super java.lang.Object class? So from every class in java, we can potentially create an object. It can be Person or it can be Student or it can be Car and so on and if we think about these classes they are all objects. Java wants to give generic behaviors for every object that ever will be created in java. The java.lang.Object has 11 methods(Java 8) so every class will inherit these methods.
equals(Object obj)
This is one of the methods that will come from the Object class. We need the equals method to compare two objects of the same class on equality. So our superclass is giving as equals method to do so. By default, it will not compare two object properties, it will compare if two references are pointing to the same object or not(same as ==). We need to override the equals method and write the logic of how exactly we want to compare our objects.
It’s good to have some common methods for all objects because other libraries can assume that in order to compare your objects they can use the equals method. The same logic for other methods as well.
We are saying that every class extends Object but in this example, our Student class extends our Person class, not the Object class. Yes, the Student class will extend the Object class via the Person class.
Java allows only a single inheritance type. Multiple classes can inherit from a single class but one class cannot inherit multiple classes at the same time.
Method overriding and variables hiding
When a child class wants to use its own implementation of the method instead of the parent class method, it can always override it.
// in Car.java file
public class Car {
public void drive() {
System.out.println("Car is driving");
}
}// in BMW.java file
public class BMW extends Car {
@Override
public void drive() {
System.out.println("BMW is driving");
} public static void main(String[] args) {
BMW bmw = new BMW();
bmw.drive(); // BMW is driving
}
}
- in the above example, the child class BMW overrides the method of a parent class.
@Override
annotation will check if we are really overriding the method. If it finds some issues, it gives a compiler error.
Rules of overriding:
- Method name and number, order, and type of arguments should be exactly the same as the parent’s method.
- The return type should be the same or covariant with the parent method.
- The access modifier should be the same or more visible than the parent method.
- If exception declaration exists in the parent method, the child method can have the same type of exception declaration or a smaller type.
Why do some people refer to overriding as runtime polymorphism?
Car car = new BMW();
car.drive(); // BMW is driving
In the above example, we have Car
as our reference type and it will decide what method and variables are available for this reference. The actual object of car
is BMW
. During the compilation, java thinks that it will call drive()
method from Car
class, but during the runtime, it figures out that BMW
actually overrides the method and it will call the method from BMW
Now, for variables, if child class will have variables with exact same name as parent class does. Child class variables will hide the parent class variables.
Hiding is different than overriding, there is no runtime polymorphism.
// in Car.java file
public class Car {
public String name = "CAR";
}// in BMW.java file
public class BMW extends Car {
public String name = "BMW"; public static void main(String[] args) {
BMW bmw = new BMW();
System.out.println(bmw.name); // BMW
Car car = new BMW();
System.out.println(car.name); // CAR
}
}
this and super keywords
this
keyword is used to refer to the current object of a class.
super
keyword is used to refer to the parent class members.
// in Car.java file
public class Car {
public String name = "CAR";
}// in BMW.java file
public class BMW extends Car {
public String name = "BMW"; public void printName() {
System.out.println(this.name); // BMW
System.out.println(super.name); // CAR
} public static void main(String[] args) {
BMW bmw = new BMW();
bmw.printName(); }
}
- As you can see in the
printName()
method we are usingsuper
to get thename
of a parent class. - It works similarly with methods.
To call a constructor we can also use this
and for parent class constructor super
. Let’s see examples of calling empty constructors.
this(); // calls empty constructor of current class
super(); // calls empty constructor of parent class
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Design Patterns
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET