Inheritance in Java
How inheritance works
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.
Listing 1. Public Person
class
public 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.
Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.
What is inheritance in object oriented programming?
What is meant by multiple inheritance?
What is the single inheritance?
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.
What Is syntax in Java?
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.
What is class inheritance?
Can private classes be inherited in Java?
class hierarchy
Now suppose you have an 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.
Single versus multiple inheritance
Languages like C++ support the concept of multiple inheritance: At any point in the hierarchy, a class can directly inherit from one or more classes. The Java language supports only single inheritance— meaning you can only use the 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