Java OOP Exam Questions & Answers
Java OOP Exam Questions & Answers
Method overloading in Java allows a class to have more than one method with the same name, as long as their parameter lists differ. This feature enhances flexibility by allowing different methods to be called using the same name but with different arguments. This is exemplified in the Calculator class where the 'add' method is overloaded to handle two or three integer parameters . It improves code readability and provides multiple ways to perform an operation without knowing exact details about the number of arguments at compile time .
Aggregation is considered a weak form of association in object-oriented design because it represents a 'has-a' relationship where the contained object can exist independently of the container. This means the lifecycle of the aggregated object does not depend on that of the container, allowing for more flexible and modular design. For example, if a Department object contains multiple Employee objects, the Employee objects can exist independently outside of the Department object. This contrasts with composition, where the lifecycles are strictly tied together .
The 'super' keyword is used in Java to refer to the immediate parent class object or constructor. It is necessary when you want to call a parent class's constructor in a derived class to ensure that the base class is properly initialized before any additional operations are performed in the derived class's constructor. For instance, in the Employee class, the 'super()' call in the constructor calls the Person class constructor first, maintaining a proper inheritance structure .
Using interfaces in Java ensures full abstraction by allowing the definition of methods without implementing their actual functionality. This mandates that any class implementing the interface provide concrete implementations for all defined methods, thereby enforcing a contract for consistent API behavior. Interfaces enhance flexibility in design as they enable multiple inheritance of types and allow a class to implement multiple interfaces. For example, the Shape interface specifies the 'area' method, which must be implemented by any class like Rectangle that implements Shape .
Polymorphism in Java, particularly runtime polymorphism, plays a crucial role in achieving abstraction by allowing objects to be treated as instances of their parent class, thus enabling a single interface to represent different underlying forms (data types). It is implemented through method overriding, where a subclass provides a specific implementation of a method already defined in its parent class. In the Dog class, overriding the 'sound' method of the Animal class demonstrates runtime polymorphism, as the actual method call to 'sound' is resolved at runtime .
Method overriding occurs in Java when a subclass provides a specific implementation for a method already defined in its superclass, allowing for runtime polymorphism. This contrasts with method overloading, where multiple methods have the same name but different parameter lists within the same class. The implications include allowing subclasses to provide specific behaviors while maintaining a consistent interface through overriding, and enabling methods to be used interchangeably while customizing functionality through overloading. For example, the Dog class overrides the 'sound' method from the Animal class to customize the behavior .
The 'this' keyword in Java is used within class methods to reference the current object—the current instance of the class. It helps in differentiating between instance variables and parameters or for passing the current instance as a parameter to another method or constructor. For instance, in the Student class, 'this' is used in the setName and setId methods to distinguish between the method parameters and class fields with the same name .
Static methods in Java belong to the class rather than any particular instance and can be called without creating an instance of the class. They cannot access instance variables or methods directly; instead, they operate solely on static data. In contrast, non-static methods require an instance of their class and can access both static and instance data. Static methods enhance performance and clarity when behavior cannot depend on instance-specific data, but they limit flexibility and extensibility compared to non-static methods which can be overridden in subclasses. For instance, static methods can only access other static members or accept class-based access through objects .
Encapsulation in Java is defined as the bundling of data with the methods that operate on that data. It is typically implemented using private variables along with public getter and setter methods to access and update these variables safely . For example, in the Student class, the variables 'name' and 'id' are private and accessed through public methods getName, setName, getId, and setId .
Inheritance in Java allows a class (subclass) to inherit fields and methods from another class (superclass), facilitating code reuse and establishing a logical relationship between classes. However, Java does not support multiple inheritance, where a class can inherit from more than one class, as this can lead to complexity and ambiguity in method selection, known as the diamond problem . Instead, multiple types of inheritance in Java are achieved through interfaces which allow a class to implement multiple interfaces, effectively simulating multiple inheritance without its drawbacks .