From the course: Oracle Java Certification: 3. Methods and Inheritance
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Inheritance and polymorphism - Java Tutorial
From the course: Oracle Java Certification: 3. Methods and Inheritance
Inheritance and polymorphism
- [Instructor] Inheritance promotes code reuse by allowing classes to share member definitions. A subclass can inherit methods from superclasses, or redefine them. As a result different subclasses with a common superclass can have different definitions for common methods. This is how polymorphism is realized. Method overriding is a key concept for understanding inheritance and polymorphism. We explore some rules for method overriding. Let's start with an example. What do you think this code would output? The Main2 class extends the Main class and overrides the message method. The overriding method on line 15 and the overridden method on line two seems to have the same signature but different return types. Would this work? Usually an overriding method has the same header as the overridden method. But Java does allow an overriding method to have a different return type as long as it is of a compatible type. Because the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.