Classes in Java code exist in hierarchies. Classes above a given class in a hierarchy are superclasses of that class. That particular class is a subclass of every class higher up the hierarchy. A subclass inherits from its superclasses. The java.lang.Object
class is at the top of the class hierarchy — so every Java class is a subclass of, and inherits from, Object
.
For example, suppose you have a Person
class that looks like the one in Listing 1.
Person
classpublic class Person {
public static final String STATE_DELIMITER = "~";
public Person() {
// Default constructor
}
public enum Gender {
MALE,
FEMALE,
UNKNOWN
}
public Person(String name, int age, int height, int weight, String eyeColor, Gender gender) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
this.eyeColor = eyeColor;
this.gender = gender;
}
private String name;
private int age;
private int height;
private int weight;
private String eyeColor;
private Gender gender;
The Person
class in Listing 1 implicitly inherits from Object
. Because inheriting from Object
is assumed for every class, you don't need to type extends Object
for every class you define. But what does it mean to say that a class inherits from its superclass? It simply means that Person
has access to the exposed variables and methods in its superclasses. In this case, Person
can see and use Object
's public and protected methods and variables.
Single inheritance enables a derived class to inherit properties and behavior from a single parent class. It allows a derived class to inherit the properties and behavior of a base class, thus enabling code reusability as well as adding new features to the existing code.
The syntax is mostly derived from C and C++. Unlike C++, Java is almost exclusively an object-oriented language. There are no global functions or variables, but there are data members which are also regarded as global variables. all code belongs to classes and all values are objects.
Employee
class that inherits from Person
. Employee
's class definition would look something like this:
public class Employee extends Person {
private String taxpayerIdentificationNumber;
private String employeeNumber;
private BigDecimal salary;
// . . .
}
The Employee
inheritance relationship to all of its superclasses (its inheritance graph) implies that Employee
has access to all public and protected variables and methods in Person
(because Employee
directly extends Person
), as well as those in Object
(because Employee
actually extends Object
, too, though indirectly). However, because Employee
and Person
are in the same package, Employee
also has access to the package-private (sometimes called friendly) variables and methods in Person
.
To go one step deeper into the class hierarchy, you could create a third class that extends Employee
:
public class Manager extends Employee {
// . . .
}
In the Java language, any class can have at most one direct superclass, but a class can have any number of subclasses. That's the most important thing to remember about inheritance hierarchy in the Java language.
extends
keyword with a single class. So the class hierarchy for any Java class always consists of a straight line all the way up to java.lang.Object
. For indepth understanding of java click on Interface in java Polymorphism in Java Exception Handling in java Multithreading in java Java Virtual MachineYou liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.