Chapter 6. Working with inheritance

Exam objectives covered in this chapter

What you need to know

[7.1] Describe inheritance and its benefits. The need for inheriting classes. How to implement inheritance using classes.
[7.2] Develop code that demonstrates the use of polymorphism; including overriding and object type versus reference type. How to implement polymorphism with classes and interfaces. How to define polymorphic or overridden methods. How to determine the valid types of the variables that can be used to refer to an object. How to determine the differences in the members of an object, which ones are accessible, and when an object is referred to using a variable of an inherited base class or an implemented interface.
[7.3] Determine when casting is necessary. The need for casting. How to cast an object to another class or an interface.
[7.4] Use super and this to access objects and constructors. How to access variables, methods, and constructors using super and this. What happens if a derived class tries to access variables of a base class when the variables aren't accessible to the derived class.
[7.5] Use abstract classes and interfaces. The role of abstract classes and interfaces in implementing polymorphism.
[9.5] Write a simple Lambda expression that consumes a Lambda Predicate expression Syntax and usage of lambda expressions. Usage of Predicate class.

All living beings inherit the characteristics and behaviors of their parents. The offspring of a fly looks and behaves like a fly, and that of a lion looks and behaves like a lion. But despite being similar to their parents, all offspring are also different and unique in their own ways. In addition, a single action may have different meanings for different beings. For example, the action “eat” has different meanings for a fly than a lion. A fly eats nectar, whereas a lion eats an antelope.

Something similar happens in Java. The concept of inheriting characteristics and behaviors from parents can be compared to classes inheriting variables and methods from a parent class. Being different and unique in one’s own way is similar to how a class can both inherit from a parent and define additional variables and methods. Single actions having different meanings can be compared to polymorphism in Java.

In the OCA Java SE 8 Programmer I exam, you’ll be asked questions on how to implement inheritance and polymorphism and how to use classes and interfaces. Hence, this chapter covers the following:

  • Understanding and implementing inheritance
  • Developing code that demonstrates the use of polymorphism
  • Differentiating between the type of a reference and an object
  • Determining when casting is required
  • Using super and this to access objects and constructors
  • Using abstract classes and interfaces
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.7.131