Loading topic
extends. A Dog is a kind of Animal, and so is a Cat. This chapter covers what that relationship lets you do with variables. In Java, a variable of type Animal can hold a Dog object, a Cat object, or a plain Animal object. Polymorphism is b5 min readAnimal can hold a Dog or a Cat. Second, a subclass can override a method it inherits. This chapter puts the two together and answers one question: when you call a method through a variable, which version of the method runs?5 min readsuper keyword for this.5 min readAnimal class had a sound() method that returned "...". That was a placeholder. A plain animal doesn't make any real sound, so the method only existed so that Dog and Cat could override it. Placeholders like this cause problems. Someone can forget to override the me5 min readDog in a variable of type Animal. That is called upcasting. Upcasting means treating an object as one of its superclasses. Java does it for you automatically, and it is always safe.5 min readObject. Object is the root class of all Java classes. Even when you write class Point with no extends, Java quietly treats it as class Point extends Object.5 min readCircle knew its own area, and a Square knew its own. This chapter shows a second way to organize the same idea. You list every kind of shape up front. Then one method handles each kind in turn, and the compiler c5 min read