Class c = new ClassA();
-
Why and why could there be different names in the definition of the object?
-
The answer to " why " is simple: because https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.5 ♪
Each class may be inherited from other classes (and interfaces) and the left part of the assignment may be a pre-class or an interface.
More interesting, why. Look, this is it. polymorial♪ It's often that a few different things have a lot in common, similar to each other, and you want to treat them the same way. For example, you have classes that are a plane and a helicopter, and you want to check for each of them if you need repair. The hold is that you announce the general parentage class of the " flying machine " , make a general functionality there. Now you can.
- To declare the list
List<>
) Your aircraft, and contain both aircraft and helicopters - To declare a function that takes place at the entrance of the aircraft and conducts the same actions for the aircraft and the helicopter (e.g. inventory).
Why, right after the designer's call, declare a narrower type of variable? Options may also be several:
- You may wish to emphasize that you treat the object as one of the groups of similar objects. For example, you can set up a plane, but you're using it as an aircraft, and you're not interested in a plane or a helicopter.
You can get access to the data you've got. http://ideone.com/F2tTRl for such classes
class Base { public String name = "Base"; }
class Derived extends Base {
public String name = "Derived";
}
access
Base.name
type referenceDerived
You won't get it.
- To declare the list