All objects are of type Object
, i.e., any given object can be represented as a Object
-type variable in addition to its own type.
String text = "text";
Object textString = "another string";
// or
String text = "text";
Object textString = text;
In addition to each variable's original type, each variable can also be represented by the types of interfaces it implements and classes that it inherits.
However, assignment in the other direction, i.e., setting an Object-type variable to a String type, will not work.
The String class inherits the Object class and, as such, String objects are always of type Object.
The Object class does not inherit a String class, so Object-type variables are not automatically of type String
The API documentation for the String class begins with a generic header followed by the class' package (java.lang
). After the package details, the name of the class (Class String
) is followed by the inheritance hierarchy of the class.
The inheritance hierarchy lists all the classes that the given class has inherited. Inherited classes are listed in the order of inheritance, with class being inspected always at the bottom.
In Java, each class can inherit one class at most.
On the other hand, the inherited class may have inherited another class. As such, a class may indirectly inherit more than a single class.
Knowledge of the fact that objects can be of many different types — of type Object, for instance — makes programming simpler.
If we only need methods defined in the Object class, such as toString
, equals
and hashCode
in a method, we can simply use Object
as the type of the method parameter.
In that case, you can pass the method for any object as a parameter.
The String
class implements the Serializable
, CharSequence
, and Comparable <String>
interfaces.
An interface is also a type. According to the class' API description, the following interfaces can be set as the type of a String object.
Serializable serializableString = "string";
CharSequence charSequenceString = "string";
Comparable<String> comparableString = "string";
Since we're able to define the type of a method's parameter, we can declare methods that receive an object that implements a specific interface. When a method's parameter is an interface, any object that implements that interface can be passed to it as an argument.
Abstract Class | Interface |
---|---|
Can contain abstract methods as well as non abstract methods ie having method definition. | Can contain only abstract methods. No definition. |
Cannot be used as reference type ie we cannot declare variables of this type. | Can be used as a reference type. |
Is used by extends keyword. |
Is used by implement keyword. |
One subclass can only use one super class. | A class can implement multiple interfaces. |
Methods are not abstract by default. | All methods are abstract by default. |
With help of non-abstract method we can define some default behavior of the concept. | Interface is truly abstract in the sense. Since there are no abstract methods, there is no default behavior. It just a concept that exists. All the implementing objects should implement those behavior themselves. |