13.13 Inheritance and Polymorphism in Java: Internal Classes and Inheritance
Object-oriented programming (OOP) is a development paradigm that allows programmers to create programs based on the concept of "objects", which can contain data and methods for manipulating that data. Java, being an object-oriented language, offers robust mechanisms for implementing inheritance and polymorphism, including the use of internal classes. In this chapter, we will explore how inheritance and polymorphism manifest themselves in the context of inner classes in Java.
Inheritance in Java
Inheritance is a fundamental principle of OOP that allows you to create a new class based on an existing class. The new class, called a derived class or subclass, inherits attributes and methods from the base class, also known as the superclass. In Java, inheritance is performed with the extends
keyword.
Internal Classes
In Java, it is possible to define a class within another class. These are called inner classes and can be categorized into four types:
- Regular inner class: A class defined within the scope of another class.
- Static inner class: An inner class that is declared as static. It can be accessed without the need for an instance of the external class.
- Local inner class: A class defined within a block of code, such as a method.
- Anonymous class: An unnamed class defined and instantiated in a single expression.
Inheritance with Internal Classes
When an inner class extends another class, it inherits its attributes and methods. This allows for code reuse and the creation of more complex class hierarchies. Additionally, an inner class can access members of its outer class, even if they are private.
Consider the following example:
public class External {
private String message = "Hello, World!";
Inner class extends OtherClass {
void displayMessage() {
System.out.println(message); // Access to the private member of the external class
}
}
}
class OtherClass {
public void someMethod() {
// Implementation
}
}
The Internal
class is an internal class of the External
class and extends OtherClass
. It can access the private member message
of the External
class without any problems.
Polymorphism
Polymorphism is another key OOP concept that allows an object to be treated as an instance of its own class or any class from which it inherits. In Java, polymorphism is generally implemented through override methods and interfaces.
When an inner class overrides a method of its superclass, it is using polymorphism to provide a specific implementation of that method. This is especially useful when we want to modify the behavior of inherited methods in specific contexts of the external class.
See the following example:
public class External {
// ... other members of the External class ...
Inner class extends OtherClass {
@Override
public void someMethod() {
System.out.println("Method overwritten in inner class.");
}
}
}
In the example above, the Internal
class overrides the someMethod
method of the OutraClass
class. When an object of the Internal
class calls someMethod
, the overridden version is executed.
Final Considerations
Inheritance and polymorphism are powerful concepts that allow you to create reusable and extensible code. Internal classes in Java provide a way to organize code more closely and integrated with the external class, while maintaining the ability to inherit from other classes and implement polymorphism.
It is important to note that although inner classes can access all members (including private) of the outer class, the outer class does not have direct access to members of the inner class. Furthermore, the use of internal classes must be well thought out, as they can complicate the design of your program if used without discretion.
Understanding how inheritance and polymorphism work in conjunction with internal classes is essential for any Java developer who wants to master the language and write efficient, organized code.