Java - Inheritance Inheritance hierarchy

Introduction

All classes in an inheritance chain form a tree-like structure, which is known as an inheritance hierarchy.

All classes above a class in the inheritance hierarchy are called ancestors for that class.

All classes below a class in the inheritance hierarchy are called descendants of that class.

Single inheritance

Java allows single inheritance for a class.

A class can have only one superclass (or parent).

A class can be the superclass for multiple classes.

Object class in hierarchy

All classes in Java have a superclass except the Object class.

The Object class sits at the top of the inheritance hierarchies for all classes in Java.

The term "immediate superclass" is one level up in the inheritance hierarchy.

The term "superclass" means an ancestor class at any level.

class Employee {
  private String name = "Unknown";

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}
class Manager extends Employee, Object{ //Error, you cannot specify two class name here
  
}
public class Main {
  public static void main(String[] args) {
    Employee emp = new Employee();
    int hashCode = emp.hashCode();
    String str = emp.toString();
    System.out.println(hashCode);
    System.out.println(str);

  }
}

Related Topics