Java Inheritance and OOP Examples
Java Inheritance and OOP Examples
Encapsulation in the `EncapTest` class provides the benefit of data protection by restricting access to private variables and exposing only specific public methods for interacting with the data, thereby enhancing security and data integrity. However, this approach can have drawbacks, such as increased overhead due to the necessity of writing additional methods for getting and setting data, which could complicate the codebase if not designed carefully. Overusing accessors can sometimes lead to breaking encapsulation by allowing excessive external manipulation, thus defeating its purpose .
In the `My_Calculation` class, method overriding is utilized by defining a `multiplication` method that is unique to this class, while it inherits the `addition` and `Substraction` methods from its superclass, `Calculation`. This demonstrates polymorphism, where a subclass can extend or modify functionalities of its superclass. The significance of method overriding in object-oriented programming includes increased flexibility and the ability to define a subclass-specific version of a method, thereby improving code reuse and clarity .
The `super` keyword is used in the `Subclass` constructor to invoke the constructor of its superclass, `Superclass`, which takes an integer `age` as an argument. This ensures that the `age` field is properly initialized in the superclass. The `super` keyword helps in calling the parent class methods and accessing its fields, ensuring that the subclass maintains the integrity of the inherited features .
Encapsulation enhances data protection by restricting direct access to some of an object's components, which can prevent unauthorized parts of a program from interfering with the object's internal state. In the provided code example, the class `EncapTest` makes the fields `name`, `idNum`, and `age` private and provides public methods `getName`, `getIdNum`, `getAge`, `setName`, `setIdNum`, and `setAge` to access and modify these fields. This encapsulation ensures that changes to the data can only be made through these controlled methods .
Polymorphism is achieved in the 'car' and 'bus' classes by method overriding. Both classes override the `display` method defined in their superclass, `Vehicle`, to provide specific implementations. When the `display` method is called on an object of type `car` or `bus`, the method unique to that subclass is executed, illustrating polymorphism. This allows the same method to behave differently based on the object's runtime type .
Setter methods in Java, such as `setAge`, `setName`, and `setIdNum`, provide controlled access to modify the value of private fields in a class. This is important for encapsulation because it allows modifications only through these methods, wherein validation checks or other logic can be implemented before changing the internal state. This helps maintain data integrity and protects the data from invalid inputs .
In the context of the `car` class' `display` method, the keyword `super` is used to call the `display` method of the superclass `Vehicle`. This is necessary to ensure that the functionality provided by the superclass's version of the `display` method is preserved and executed before adding the subclass-specific behavior. It maintains the inheritance chain and allows the subclass to build on top of the existing methods, promoting code reuse and extension .
The principle of inheritance is reflected in the design of the `Calculation` and `My_Calculation` classes through the `My_Calculation` class extending `Calculation`. This signifies that `My_Calculation` inherits methods and variables from `Calculation`, such as `addition` and `Substraction`, and is capable of adding new functionalities like `multiplication`. Inheritance allows `My_Calculation` to reuse and enhance existing code, demonstrating the 'is-a' relationship between subclasses and superclasses .
The 'car' and 'bus' classes demonstrate an inheritance relationship with the 'Vehicle' class, meaning they are subclasses or derived classes of 'Vehicle'. This implies that both 'car' and 'bus' inherit properties and methods from 'Vehicle', such as the field 'num' and the method 'display'. This is a fundamental concept of inheritance in object-oriented programming, where a subclass can reuse code from the superclass, potentially overriding existing methods or adding new ones .
The 'this' keyword is used in the constructor of the `Superclass` to differentiate between the class fields and the parameters of the constructor which have the same name. In this context, 'this.age = age;' assigns the value of the parameter `age` to the instance variable `age` of the `Superclass` object being created. This is essential when both the instance variable and the parameter name are the same to remove ambiguity .