C# Inheritance: Duster Class Example
C# Inheritance: Duster Class Example
The 'Duster' class modifies inherited properties by changing the value of 'maxSpeed' to 120 and redefining behavior for methods like 'steerRight', 'steerLeft', and 'brake'. It inherits 'noOfWheels' and 'noOfSeats' from 'Car' but alters the 'brake' implementation to demonstrate different car-specific behavior. This showcases how a subclass can tailor inherited properties and methods to better suit its specific needs .
Constructor parameters in the inheritance structure play a vital role in object instantiation by ensuring that each class in the hierarchy can initialize its fields correctly. For instance, the 'Duster' class uses 'super(a)' to pass the parameter to the 'Car' constructor, initializing 'currentSpeed'. This highlights how constructor parameters enable parameterized initialization, promoting a flexible and dynamic creation of objects where the state can be tailored from instantiation .
The 'horn' method in the 'Car' class cannot be overridden in the 'Duster' class because it is declared as 'final'. A final method cannot be overridden by subclasses, providing the method's implementation with a guarantee of immutability and consistency across different subclass usages .
The example illustrates encapsulation by defining methods and properties with appropriate access levels and using constructs like 'final' to protect method implementations from being overridden. Through encapsulation, even with method overriding and inheritance, classes can protect certain behavior from modification, control access to sensitive properties, and offer a stable interface for interaction .
Method overriding is displayed in the inheritance chain where the 'brake', 'steerRight', and 'steerLeft' methods are overridden in the 'Duster' class. The significance is that it allows a subclass to provide a specific implementation of a method that is already defined in its parent class. This facilitates polymorphism, enabling objects to be handled in a general way while displaying behavior specific to their actual class at runtime .
The 'super' keyword is used in the 'Car' and 'Duster' classes to invoke the constructor of the parent class and to access methods defined in the parent class. In the 'Car' class, 'super.brake()' calls the 'brake' method from the 'Vehicle' class, ensuring that the base class implementation is preserved. In the 'Duster' class, 'super(a)' in the constructor calls the parent 'Car' constructor to initialize 'currentSpeed', showing constructor chaining and inheritance of properties .
The 'Has-A' relationship, implemented between the 'Car' and 'Engine' classes, is an example of composition where 'Car' contains an 'Engine' object to manage engine operations like 'startEngine()' and 'stopEngine()'. This is contrasted with the 'Is-A' relationship portrayed in the inheritance hierarchy where 'Car' extends 'Vehicle' and 'Duster' extends 'Car'. The 'Has-A' relationship allows a class to utilize functionalities of another class without inheriting from it, providing a more flexible design .
Constructor chaining is crucial in initializing class instances with inherited properties across the hierarchy. In the 'Duster' class, 'super(a)' is called in its constructor to invoke the 'Car' constructor, which initializes 'currentSpeed'. This ensures that properties defined at higher levels in the hierarchy are initialized properly and consistently, allowing an orderly and coherent state establishment for objects across the inheritance chain .
Dynamic method dispatch occurs when a method call is resolved at runtime rather than compile-time, allowing for polymorphic behavior. In the provided class structure, dynamic method dispatch is demonstrated when a 'Car' reference holds a 'Duster' object (i.e., 'Car c = new Duster(10);'). Despite the reference being of type 'Car', the overridden methods in 'Duster', like 'steerLeft()', are called, demonstrating polymorphism .
The 'Duster' class is declared as final to prevent further subclassing. This implies that no class can extend 'Duster', securing its implementation from being modified or extended, which provides stability to the class design by preserving its behavior and structure .