Which features of OOP ( object-oriented programming) indicate code reusability in Java?

114    Asked by ashish_1000 in Java , Asked on Dec 22, 2023

How the various features of object-oriented programming (OOP) in the context of Java can provide help in the usability of code? 

Answered by Chloe Burgess

In the context of object-oriented programming (OOP) in Java, the correct answer for which features of OOP indicates code reusability is that several features can enhance the reusability of code:-

Inheritance:- By extending classes, new classes can inherit properties and behavior from your existing class. Here is the instance:-

Class Vehicle {
    Int numberOfWheels;
    String engineType;
    // other properties and methods
}
Class Car extends Vehicle {
    // inherits numberOfWheels and engineType
    // specific Car properties and methods
}

Polymorphism The polymorphism allows you to gain different objects to be treated as instances of the same class which can lead you to get flexibility and reusability. Here is the instance given:-

Interface Shape {
    Void draw();
}
Class Circle implements Shape {
    Public void draw() {
        // draw circle
    }
}
Class Square implements Shape {
    Public void draw() {
        // draw square
    }
}

Encapsulation This particular principle or function helps in bundling the data. It also offers a method that can operate within a single unit class. It ensures that the internal workings of a class are hidden which can lead you to get usability. Here is the instance given:-

Class Customer {
    Private String name;
    Private int age;
    // other properties and methods
}


Your Answer

Interviews

Parent Categories