Professional Documents
Culture Documents
• Composition implies a strong ownership relationship, where one class is composed of other
classes, and the child objects have no independent existence.
• Aggregation represents a weaker relationship, where one class contains another class, but
the child objects can exist independently and may be shared among multiple parents.
30. What is a virtual method in OOP, and why is it used? A virtual method is a
method in a base class that can be overridden by derived classes. It is used to enable
polymorphism, allowing different derived classes to provide their own specific
implementations of the method.
31. How does OOP promote code organization and modularization? OOP promotes
code organization by encapsulating data and behavior within classes, which serve as self-
contained units. This modularization makes it easier to manage and maintain code, as
changes can be isolated to specific classes without affecting the entire system.
32. What is the difference between a shallow copy and a deep copy in OOP, and
when would you use each? A shallow copy duplicates an object's references, creating a new
object that refers to the same underlying data as the original. A deep copy duplicates both the
object's references and the data they point to, creating entirely independent copies. The
choice between them depends on the specific requirements of your program.
33. Explain the concept of method overloading with an example. Method overloading
is the practice of defining multiple methods with the same name in a class, but with different
parameter lists. For example, you can have a "calculateArea" method in a "Shape" class that
accepts different parameters based on the shape type, allowing you to calculate the area of
various shapes using the same method name.
34. What are abstract classes and methods in OOP, and when are they used?
Abstract classes are classes that cannot be instantiated directly and are meant to be
subclassed. Abstract methods are methods declared in abstract classes without
implementation. They are used when you want to enforce that subclasses provide specific
implementations for certain methods, ensuring a common interface while allowing for
specialization.
35. How does OOP support the concept of "is-a" and "has-a" relationships between
objects? OOP supports "is-a" relationships through inheritance, where a subclass is
considered a specialized version of its superclass. "Has-a" relationships are implemented
through composition, where a class contains instances of other classes as members,
indicating that it has a relationship with those objects.