Understanding Inheritance in Java: The “Is-A” Relationship
Introduction to Inheritance
Inheritance in Java is a fundamental concept of object-oriented programming that allows a new class to inherit properties and methods from an existing class. This relationship is often described as an “Is-A” relationship, indicating that one class is a specialized form of another class.
Establishing Inheritance through “Is-A” Relationship
The ‘Is-A’ (Inheritance) relationship in Java expresses that one class (the subclass) is a specific type of another class (the superclass). For instance, consider the Integers
class and the EvenNumbers
class. In this context, it’s accurate to say that an EvenNumber
‘Is-A’ Integer
, since every even number inherently qualifies as an integer. Consequently, we can establish an inheritance relationship between the EvenNumber
class and the Integer
class to signify that all properties true for Integers
are also true for EvenNumbers
. This concept mirrors the relationship between a superset and a subset in set theory. If you think of Integers
as a superset, then EvenNumbers
represent a subset of this superset.
Example: ‘Integers’ and ‘EvenNumber’ Classes
Let’s create two classes to illustrate this concept:
- Integers Class (SuperClass): This class represents all integers and includes a method
findIfInteger
to demonstrate some operation on integers. - EvenNumbers Class (SubClass): This class represents even numbers and includes a method
findIfEvenNumber
. It inherits from theIntegers
class.
Code Implementation:
public class Integers {
// Method to check if a number is an integer
public boolean findIfInteger(int number) {
return true; // Since every number provided to this method is an integer
}
}
public class EvenNumbers extends Integers {
// Method to check if an integer is an even number
public boolean findIfEvenNumber(int number) {
return number % 2 == 0;
}
}
public class Main {
public static void main(String[] args) {
EvenNumbers evenNum = new EvenNumbers();
// Using method from the superclass (Integers)
System.out.println("Is 5 an integer? " + evenNum.findIfInteger(5));
// Using method from the subclass (EvenNumbers)
System.out.println("Is 5 an even number? " + evenNum.findIfEvenNumber(5));
}
}
Explanation of the Code:
- In the
Integers
class, the methodfindIfInteger
simply returns true for any number, as all are integers. - The
EvenNumbers
class extendsIntegers
, inheriting its methods. It also defines an additional methodfindIfEvenNumber
to check if a number is even. - In the
main
method, an object ofEvenNumbers
is created. Through this object, we can call both the inherited method (findIfInteger
) and the method defined inEvenNumbers
(findIfEvenNumber
).
Conclusion
In this article, we explored how Java’s inheritance models the “Is-A” relationship, drawing an analogy with set theory’s superset and subset concept. We demonstrated this with the Integers
and EvenNumbers
classes, highlighting the practical implementation of inheritance in Java. This concept is pivotal in understanding object-oriented programming and designing efficient, reusable code.