Although all classes in Java are sub-classes of Object class, but different from other object types, a reference variable of type Object can\'t be assigned to any other refe
Once you've assigned tiger
to an Object
reference, the compiler no longer knows that it's actually a Tiger
. All it knows is that the tiger
object is an Object
-- it might be a String
, Integer
, Fruitcake
, or Animal
.
This is exactly how it's supposed to work.
Given the statement: Object o = new Tiger();
The type of the variable (Object) controls the interactions (methods) available to you while the type of the value (Tiger) controls the behavior that is executed for a given interaction.
By using an Object reference for a Tiger instance as in this example, you're saying that you want to use an instance of Tiger as an Object. Well, an object doesn't have the method Scream()
so it's not going to give you that option. If you want to be able to Scream()
, use it as a type that actually knows how to Scream()
like Animal
or Mammal
btw, methods should start with a lowercase
Since Scream()
is not declared as a public method of Object
, you can't call it on a variable of type Object
. The compiler only knows the static type of variables (references), so it can't tell nor verify that this reference is going to point to an object of type Tiger
at runtime (which would be its dynamic type). This is why you get a compilation error.
You may say it would be trivial for the compiler to verify the dynamic type in the case above, which is more or less correct. However, in real life, it is usually not so simple. Consider
Factory factory = new SomeFactory();
Object obj = factory.getProduct(param);
// What is obj's dynamic type?
The compiler could in theory detect that factory
's dynamic type is SomeFactory
. Then all it should do is to statically analyse the code within SomeFactory.getProduct()
to detect the type of the product returned. Of course, getProduct
may return different types of actual products depending on its parameters. So the compiler would need to detect the possible value(s) of param
at the point of invocation... Which may come from any number of sources, e.g. user input, config file, DB... But even if this was all surmountable, the concrete product class may be loaded dynamically by the class loader at runtime, from a jar file which may not even be available at compile time. So the compiler may not even be aware of the existence of this specific class, much less of its public interface.
you wanna know why we use explicit type cast. this is all about Inheritance -
Let me clear this - Let suppose we have two class Class A and Class B. And Class B is a sub class of class A. That means Class B has all functionality of Class A, this means Class B can do anything what Class A can do. So if
A a = new B();
is perfectly fine because Class B can do what class A can do. Here reference varaible a is of type Class A, from a all the method of Class A will be invokable.Now Object of B(new B();) has all the functionality of Class A (as Inheritance) so all method of Class A will be invokable.
If we reverse the condition like this ->
B b =new A();
Not possible, because may be Class B implement its own functionality(Methods and variables) which are not in Class A. But here reference variable is of type B, all the functionality of Class B will be invokable, But the actual object is of type A so Error will occur.
Same case is with Class Object.As all the other Class automatically inherit Object Class.
so When any object having reference variable of type Class Object need explicit Cast, Because this is not sure what more or less functionality that object contain.