In a statically typed language such as Java, from what I have learned, type declaration is essentially a way to catch compilation errors, an obvious advantage over dynamically typed languages. But, looking at the time when Java completes the binding, we get errors, such as ClassCastExceptionshowing how declared types relate somehow at runtime. But why does the declared type really matter?
For example:
public class TestClass
{
public static void main(String[] args)
{
Animal d = new Animal();
((Dog)d).bark();
}
}
class Dog extends Animal{}
class Animal
{
void bark()
{
System.out.println("Woof");
}
}
, , , . , Java cast call bark , Animal? , , , Java (.. woofs, !) , Java .
: , , . ? , , .