• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Inheritance in Java

Inheritance in Java

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind inheritance in java is that you can create new classes that are built upon existing classes.

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.

class A{  
void msg(){System.out.println("Hello");}  
}  
class B{  
void msg(){System.out.println("Welcome");}
}  
class C extends A,B{//suppose if it were     
 Public Static void main(String args[]){  
   C obj=new C();  
  obj.msg();//Now which msg() method would be invoked?  
}  
}

What is inheritance in object oriented programming?

Inheritance (OOP) is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior).

What is meant by multiple inheritance?

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.

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?

In object-oriented programming, inheritance enables new objects to take on the properties of existing objects. A class that is used as the basis for inheritance is called a superclass or base class. A class that inherits from a superclass is called a subclass or derived class.

Can private classes be inherited in Java?

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all theprivate members of its enclosing class—both fields and methods.

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

Interface in java

Polymorphism in Java

Exception Handling in java

Multithreading in java

Java Virtual Machine

Summary
Review Date
Reviewed Item
Inheritance in Java
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Inheritance in Java"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.