Azienda coinvolta
Explain Object-Oriented Programming (OOP) principles with examples.
Anonimo
Object-Oriented Programming (OOP) is a paradigm that organizes code into objects. It has four main principles: Encapsulation: Hiding data within a class and exposing only necessary details. Example: A BankAccount class with private balance and public deposit() and withdraw() methods. Abstraction: Hiding complex implementation and exposing only essential functionality. Example: A Car class with a startEngine() method that internally handles ignition and fuel injection. Inheritance: Allowing one class to inherit properties and methods from another. Example: A Dog class inheriting from Animal class, reusing eat() but adding bark(). Polymorphism: Allowing methods to take different forms. Example: Method overloading (add(int, int) and add(double, double)) and method overriding (run() method in Cheetah and Tortoise classes).