Chapter 02: Object Oriented Principles in Java
High-level overview of core object-oriented concepts in Java, including inheritance, polymorphism, abstract classes, interfaces, and casting.
This chapter builds on the basics from Chapter 01 and focuses on how Java implements object-oriented principles in practice. You will learn to design related classes, reuse behavior through inheritance, and use polymorphism to write flexible, maintainable code.
By the end of this chapter, you should be able to explain how inheritance works in Java, how overriding and overloading differ, and how abstract classes and interfaces define contracts for your designs.
2.1 Review of Object-Oriented Principles
You briefly revisit the four fundamental pillars of object-oriented programming and see how they appear in Java syntax and real examples.
- Encapsulation and information hiding
- Inheritance and code reuse
- Polymorphism and dynamic behavior
- Abstraction and modeling real-world concepts
2.2 Super Class, Sub Class, Inheritance, and Member Access
This section explains how a subclass can extend a superclass to reuse fields and methods, and how access modifiers control visibility between classes and packages.
- Defining base (super) classes and derived (sub) classes
- Single inheritance model in Java
- Member access with
public,protected,private, and package-private - When to favor composition over inheritance
2.3 Types of Inheritance
You explore the different conceptual types of inheritance used in object-oriented design and how Java supports or restricts them.
- Single, multilevel, and hierarchical inheritance
- Why Java does not support multiple inheritance of classes
- Role of interfaces in achieving multiple inheritance of type
2.4 extends and super Keyword
This section focuses on the actual syntax that connects classes in an inheritance hierarchy and allows subclasses to refer to their superclass.
- Using
extendsto declare a subclass - Calling superclass constructors and methods with
super - Shadowing vs. overriding fields and methods
2.5 Overriding and Overloading
Here you distinguish between redefining behavior in subclasses (overriding) and defining multiple methods with the same name but different parameters (overloading).
- Method overriding and dynamic dispatch at runtime
- Method overloading and compile-time resolution
- Guidelines to use overriding and overloading effectively
2.6 Final Classes and Methods
This section explains how the final keyword is used to prevent inheritance or further overriding, and when such restrictions are useful.
- Declaring final variables, methods, and classes
- Designing stable base classes and constants
- Impact of
finalon extensibility and testing
2.7 Abstract Classes and Methods
You learn how abstract classes define partial implementations and shared structure, leaving specific details to concrete subclasses.
- Declaring abstract classes and abstract methods
- When to choose an abstract class vs. an interface
- Template-style designs using abstract methods
2.8 Upcasting vs Downcasting
This section discusses how references to objects can be converted between supertype and subtype forms and what that means for available methods.
- Safe upcasting to treat objects as their supertype
- When and how to use downcasting with
instanceof - Polymorphic behavior through base-class references
2.9 Interfaces and Implementations
Finally, you see how interfaces define contracts that multiple classes can implement, enabling loose coupling and flexible designs.
- Declaring interfaces and implementing them in classes
- Multiple interface implementation as a form of multiple inheritance
- Using interfaces to design pluggable components