100% found this document useful (1 vote)
444 views6 pages

C# Inheritance: Duster Class Example

The document extends the Vehicle class hierarchy to include Car and Duster classes. Car inherits and overrides some properties and methods from Vehicle, and implements a relationship with the Engine class. Duster inherits and overrides properties and methods from Car. Dynamic method dispatch is demonstrated. The Duster class is made final to prevent further extension, and one Car method is made final to prevent overriding.

Uploaded by

N-raj Yadav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
444 views6 pages

C# Inheritance: Duster Class Example

The document extends the Vehicle class hierarchy to include Car and Duster classes. Car inherits and overrides some properties and methods from Vehicle, and implements a relationship with the Engine class. Duster inherits and overrides properties and methods from Car. Dynamic method dispatch is demonstrated. The Duster class is made final to prevent further extension, and one Car method is made final to prevent overriding.

Uploaded by

N-raj Yadav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Extend the Vehicle class by Car class and Car class by Duster class.

Inherit the properties

From Vehicle and add new properties in Car and Duster.

2. Show is use of super keyword in Car and Duster.

3. Replace some existing properties of Vehicle in Car and Replace the some existing properties

Of Car in Duster.

4. Show the use of Dynamic Method Dispatch in above inheritance.

5. Restrict the class Duster to extend further.

6. Restrict any of the method of Car to override.

7. Implement the Has-A relationship in between Car class and Engine class

Public void

Class Vehicle{

Int maxspeed=0;

Int noOfWheels;

Int noOfSeats;

Public void brake() {

[Link](“Apply Brake”);

Class Car extends Vehicle{

Int maxspeed=100;

Int currentSpeed;

Int noOfWheels=4;

Int noOfSeats=4;
Car(int a){

[Link]=a;

Public void carStartEngine() { // implementing Has-A relationship between car and


Engine.

Engine carEngine=new Engine();

[Link]();

[Link]();

Public void steerRight() {

[Link](“turn right”);

Public void steerLeft() {

[Link](“turn left”);

Final void horn() { // A final method can be inherited but not overriden . if we try to override
final method in child we will get compiletime error.

[Link](“press Horn”);
}

Public void speed() {

[Link](currentSpeed);

@Override

Public void brake() {

// TODO Auto-generated method stub

[Link](); // super is used in brake method of car to give


same impolementation as parent vehicle

Final class Duster extends Car{

String type=”SUV”;

Int maxSpeed=120;

Duster(int a){

Super(a); // using super in subclass constructor to call parent constructor. It


will inherit property from car.

// using this super to initialize currentspeed of Duater

@Override
Public void steerRight() {

// TODO Auto-generated method stub

[Link](“Duster turns right”);

@Override

Public void steerLeft() {

// TODO Auto-generated method stub

[Link](“duster turns left”);

@Override

Public void brake() {

// TODO Auto-generated method stub

[Link](“duster apply brakes”);

@Override

Public void speed() {

// TODO Auto-generated method stub

[Link](“Speed of duster:-“);

[Link]();
}

// public void horn() { // getting compile time error as its declared final in parent car class

//

// [Link](“press Horn”);

// }

//

Class Engine{

Public void startEngine() {

[Link](“Start ENgineS”);

Public void stopEngine() {

[Link](“Stop EngineS”);

Public class Question1 {

Public static void main(String[] args) {

// TODO Auto-generated method stub

Duster a =new Duster(50);


[Link]([Link]);

[Link]();

[Link]();

Car c=new Duster(10);

[Link]();

[Link]([Link]);

Common questions

Powered by AI

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 .

You might also like