Looking at Oracle's documentation there are 3 hierarchies in Java
- Class Hierarchy
- Interface Hierarchy
- Enum Hierarchy
Can someone please explain what does each Hierarchy represent and what is their significance?
A subquestion-
It is documented that
HashMap is the Hash table based implementation of the Map interface
as documented in Oracle website HashTable & HashMap falls under Class Hierarchy and Map falls under Interface Hierarchy, then why isn't HashMap, Hashtable and Map represented under same Hierarchy?
CodePudding user response:
Interface hierarchy means there's a base class (an interface) of which you can implement your own classes. Those implemented have to have the same methods overridden as the base.
Class hierarchy means there's also a base class of which you can extend your own classes. Those will yield the same behaviour as the base class.
The difference between them is that you have to implement an interface's methods with the same parameters as given, but you will already have the base class' methods in your child class. It's useful for bigger projects where you have to code with complicated inheritance and so on.
To provide an example:
public interface IPerson{
void setPerson(int age, String name);
int getAge();
String getName();
}
public class Person implements IPerson{
private int age;
private String name;
//you have to override every implemented method
@Override
public void setPerson(int age, String name){
this.age = age;
this.name = name;
}
@Override
public int getAge(){return this.age;}
@Override
public String getName(){return this.name;}
//does the same as setPerson, just for demonstrational purposes
public Person(int age, String name){
this.age = age;
this.name = name;
}
}
public class Student extends Person{
private String school;
//you don't need to implement the same methods as in the Person class
public String getSchool(){return this.school;}
public Student(int age, String name, String school){
super(age,name); // calls Person's constructor, mandatory step
this.school = school;
}
}
public class Main{
public static void main(String[] args){
Student student = new Student(15,"Billy","good school");
// will possess the same attributes of both Person and Student
System.out.println(student.getSchool()); // "good school"
System.out.println(student.getAge()); // 15 -- you can also do this because the Student class automatically has the getAge() method because of Person
student.setPerson(16,"Billy");
System.out.println(student.getAge()); // 16
Person person = new Person(30, "Anna");
System.out.println(person.getName()); // "Anna"
System.out.println(person.getSchool()); // DOES NOT WORK
person.setPerson(29, "Annabelle");
System.out.println(person.getName()); // "Annabelle"
IPerson iperson = new IPerson(); // DOES NOT WORK
}
}
CodePudding user response:
Each of the things you mentioned are different things. A class, interface and enum interact with each other but are different. The hierarchy being referenced are how each of these things inherit from parent objects.
In Java an interface defines an set of API's that can have multiple implementations. In the case reference, Map is the set of methods that should be available on all implementations. The HashTable and HashMap are have different implementations for that interface - each with their pros and cons.
Best practice is to declare your variable with the interface and while you will specify the specific implementation when you instantiate the object, the using code should care exactly what that implementation is.
