Class Design Java 8 Certificationis another important chapter for when appearing for OCA (Oracle Certified Associate Exam 1Z0-808), (or for Java 8 Certification). At its core, proper Java class design is about code re-usability, increased functionality, and standardisation. For example, by creating a new class that extends an existing class,you may gain access to a slew of inherited primitives, objects, and methods. Alternatively,by designing a standard interface for your application, you ensure that any class that implementsthe interface has certain required methods defined. Finally, by creating abstract classdefinitions, youre defining a platform that other developers can extend and build on top of.In thisblog, Ill take things one step further and show how class structure is one of the mostpowerful features in the Java language.
Working with Inheritance
- Describe inheritance and its benefits
- Develop code that demonstrates the use of polymorphism; including overriding and object type versus reference type
- Determine when casting is necessary
- Use super and this to access objects and constructors
- Use abstract classes and interfaces
Read the full java certification syllabus article by clicking here.
Introducing Class Inheritance
When creating a new class in Java, you can defi ne the class to inherit from an existing class.Inheritance is the process by which the new child subclass automatically includes anypublic or protected primitives, objects, or methods defined in the parent class. For illustrative purposes, we refer to any class that inherits from another class as a childclass, or a descendent of that class. Alternatively, we refer to the class that the child inheritsfrom as the parent class, or an ancestor of the class. If child X inherits from class Y, whichin turn inherits from class Z, then X would be considered an indirect child, or descendent,of class Z. Java supports single inheritance, by which a class may inherit from only one direct parentclass. Java also supports multiple levels of inheritance, by which one class may extendanother class, which in turn extends another class. You can extend a class any number of times, allowing each descendent to gain access to its ancestors members.To truly understand single inheritance, it may helpful to contrast it with multiple inheritance,by which a class may have multiple direct parents. By design, Java doesnt support multiple inheritance in the language because studies have shown that multiple inheritancecan lead to complex, often difficult-to-maintain code. Java does allow one exception to thesingle inheritance rule: classes may implement multiple interfaces, as youll see later in this section.