OOPS Concept Part 2 Revision - Class
OOPS Concept Part 2 Revision - Class
A class in Java serves as a blueprint for creating objects. It bundles data (attributes) and behavior (methods) that define the
object. Consider a ‘Car’ class; it defines attributes like color and methods like ‘accelerate()’. Essentially, a class is a template
that characterizes what can be instantiated as an object, exemplifying OOPs concepts.
Abstraction in Java involves concealing complexity while exposing only the essential aspects. It’s realized through abstract
classes and interfaces. For instance, a ‘Vehicle’ interface declares a ‘move()’ method, but the actual implementation is left to
classes like ‘Car’ or ‘Bike’. This focus on “what an object does” rather than “how it does it” is a core OOPs concept in Java.
Inheritance is a mechanism where a new class (subclass) derives attributes and methods from an existing class
(superclass). This fosters code reusability and establishes a hierarchical relationship between classes. For example, an
‘ElectricCar’ class can inherit traits from the ‘Car’ class, showcasing the practicality of Java OOPs concepts.
Polymorphism allows Java methods and objects to assume multiple forms. It finds common use in method overloading
(same method name, different parameters) and method overriding (same method name and parameters in subclass as in
parent class). Consider a ‘draw()’ method implemented differently in ‘Circle’, ‘Square’, and ‘Triangle’ classes as a prime
example of polymorphism in Java.
Encapsulation is a pivotal OOPs principle in Java. It entails bundling data (attributes) and code (methods) into a cohesive
unit. Moreover, it limits direct access to an object’s components, preventing inadvertent interference. Techniques like using
private variables and offering public getter and setter methods exemplify Java OOPs concepts put into practice.
Polymorphism refers to many forms, or it is a process that performs a single action
in different ways. It occurs when we have many classes related to each other by
inheritance.
Dynamic polymorphism
In dynamic polymorphism method of the program
binds with an object at runtime the advantage of
dynamic polymorphism is allocating the memory
space for the method (either for overloaded method or
for override method) at run time.
In this diagram the sum method which is present in
BC class is called original form and the sum()
method which are present in DC1 and DC2 are
called overridden form hence Sum() method is
originally available in only one form and it is further
implemented in multiple forms. Hence Sum()
method is one of the polymorphism method.
Question 3:What is the primary mechanism that allows runtime polymorphism in Java?
a) Method Overloading
b) Method Overriding
c) Static Binding
d) Final Methods
e) Abstract Classes
Question 3:What is the primary mechanism that allows runtime polymorphism in Java?
a) Method Overloading
b) Method Overriding
c) Static Binding
d) Final Methods
e) Abstract Classes
Correct Answer: b) Method Overriding
Explanation:b) Method Overriding is correct because runtime polymorphism in Java is achieved through method
overriding. When a subclass provides its own implementation of a method that is already defined in its superclass,
and an instance of the subclass is used with a superclass reference, the overridden method in the subclass is
invoked at runtime. This dynamic behavior is the essence of runtime polymorphism.
Option A is incorrect because the method in the superclass will not be called unless explicitly called using
super.method().
Option C is incorrect because the reference type doesn’t need to match; the actual object's type matters.
Option D is incorrect as it applies to the compile-time method resolution, but polymorphism is resolved at
runtime.
Option E is incorrect because the final keyword prevents overriding, but the subclass method will still be called
if the method isn't final.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
#include <iostream>
class Solution {
public:
void func(int x) { std::cout << "value of x is " << x << std::endl; }
int main() {
Solution obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85, 64);
return 0;
}
Operator overloading is a programming language feature that allows the same operator
to be used with different meanings or behaviors depending on the types of operands
involved. It allows developers to redefine the behavior of operators for custom types or
classes.
class Calculator {
public void add(int a, long b) {
System.out.println("int-long");
}
class Calculator {
public void add(int a, long b) {
System.out.println("int-long");
}
Dynamic binding always says create an object of base class but do not
create the object of derived classes. Dynamic binding principal is always used
for executing polymorphic applications.
Correct Answer: c) Dynamic binding applies to method calls where the method is resolved at runtime based on
the actual object type.
Explanation:
c) Dynamic binding applies to method calls where the method is resolved at runtime based on the actual object
type is correct. In Java, dynamic binding occurs when a method call is made on an object, and the method that gets
executed is determined at runtime based on the actual class of the object, not the reference type.
Why the other options are incorrect:
a) Dynamic binding happens at compile-time: This is false because dynamic binding happens at runtime, not compile-
time.
b) Dynamic binding is only applicable to static methods: Static methods are resolved at compile-time (static binding),
not runtime.
d) Dynamic binding does not support method overriding: Dynamic binding is specifically tied to method overriding,
where subclass methods override parent methods.
e) Dynamic binding can be used for instance variables: Instance variables are resolved at compile-time based on the
reference type, not at runtime, so dynamic binding is not applicable to variables.
Encapsulation is one of the concepts in OOPs concepts; it is the
process that binds together the data and code into a single unit and
keeps both from being safe from outside interference and misuse. In
this process, the data is hidden from other classes and can be
accessed only through the current class’s methods. Hence, it is also
known as data hiding. Encapsulation acts as a protective wrapper that
prevents the code and data from being accessed by outsiders. These
are controlled through a well-defined interface.
Correct Answer: a) Hiding the internal state of an object and requiring all interactions to be performed through an object's
methods.
Explanation:
a) Hiding the internal state of an object and requiring all interactions to be performed through an object's methods is the correct
definition of encapsulation. It ensures that the internal workings of an object are hidden from outside interference, and data can only be
accessed or modified through well-defined methods.
Why the other options are incorrect:
b) Allowing the direct modification of an object’s data members: This contradicts the principle of encapsulation. Direct modification
of data violates data hiding.
c) Using inheritance to share methods between classes: This is related to inheritance, not encapsulation. Encapsulation focuses on
data hiding, not sharing methods.
d) Overloading methods to provide multiple ways of performing an operation: Method overloading is a feature of polymorphism, not
encapsulation.
e) Implementing multiple interfaces to achieve polymorphism: This relates to polymorphism, not encapsulation.
Question 10:What is a primary benefit of encapsulation in object-oriented
programming?
a) It increases code readability by allowing all member variables to be public.
b) It improves security by restricting direct access to the object's data members.
c) It enhances performance by allowing faster access to member variables.
d) It simplifies inheritance by making all variables accessible to subclasses.
e) It allows the programmer to bypass data validation checks for faster
development.
Question 10:What is a primary benefit of encapsulation in object-oriented programming?
a) It increases code readability by allowing all member variables to be public.
b) It improves security by restricting direct access to the object's data members.
c) It enhances performance by allowing faster access to member variables.
d) It simplifies inheritance by making all variables accessible to subclasses.
e) It allows the programmer to bypass data validation checks for faster development.
Correct Answer: b) It improves security by restricting direct access to the object's data members.
Explanation:
b) It improves security by restricting direct access to the object's data members is correct because
encapsulation provides control over how data is accessed or modified, reducing the risk of unintended or
malicious interference.
Why the other options are incorrect:
a) It increases code readability by allowing all member variables to be public: Making member
variables public defeats the purpose of encapsulation and doesn't necessarily improve readability.
c) It enhances performance by allowing faster access to member variables: Encapsulation may
slightly reduce performance due to method calls, but it prioritizes security and proper data handling over
speed.
d) It simplifies inheritance by making all variables accessible to subclasses: Encapsulation often
restricts access to variables, and while inheritance can access protected members, encapsulation enforces
control over public access.
e) It allows the programmer to bypass data validation checks for faster development: Encapsulation
encourages the use of methods that include data validation, which cannot be bypassed if data is properly
encapsulated.
Abstraction is the art of building things in such a way that the people using
them should never need to care about how they work, but instead should
only care about the outcomes they offer and certain inputs they require.
Abstractions are everywhere. A simple light switch is an easy example. It
requires minimum effort to learn what a wall mounted button does - if you
switch it on, which is achieved by positioning the button in a certain way -
the light goes on. If positioned the other way, it goes off. If one never
studies basic electric cirtcuits, they might never know what goes inside the
switch. But the most important thing is that it does not matter - knowing or
not knowing how a switch works does not affect its output and that is the
absolute beauty of abstraction.
There are also abstract classes and abstract
methods.
An abstract class is a type of class that declares one
or more abstract methods. An abstract method is a
method that has a method definition but not
implementation. Once we have modelled our object
using data abstraction, the same sets of data can
also be used in different applications—abstract
classes, generic types of behaviors and object-
oriented programming hierarchy. Abstract methods
are used when two or more subclasses do the same
task in different ways and through different
implementations. An abstract class can have both
methods, i.e., abstract methods and regular methods.
In general, when we think about Object-oriented programming, it
is everything around the “Objects” and their relationships.
Correct Answer: c) Aggregation implies a "has-a" relationship where the parts can exist
independently of the whole.
Explanation:
c) Aggregation implies a "has-a" relationship where the parts can exist independently of the whole
is the correct answer because, in aggregation, one object can be part of another, but they maintain their
own lifecycles. For example, a library and books: the books can exist independently of the library.
Why the other options are incorrect:
a) An object cannot exist without the existence of another object it is aggregated with: This describes
composition, not aggregation. In aggregation, the objects can exist independently.
b) Objects are strongly bound to each other and are dependent on one another for their lifecycle:
This is also a description of composition, where objects are tightly coupled.
d) Objects in aggregation cannot belong to more than one object at the same time: In aggregation,
objects can belong to more than one entity simultaneously. For example, the same person can be
associated with multiple departments in an organization.
e) Aggregation is a specialized form of composition where the child object is created only within the
parent class: This is a description of composition, where the parent creates and owns the child object.
Question 14:
In a composition relationship, which of the following is true?
a) The child object can exist independently of the parent object.
b) The child object is shared between multiple parent objects.
c) The child object's lifecycle is managed by the parent object and it is destroyed when the parent object is
destroyed.
d) The relationship between parent and child objects is weak, and they can exist independently of one another.
e) Composition implies that the child object will never be destroyed, regardless of the parent object's lifecycle.
Question 14:
In a composition relationship, which of the following is true?
a) The child object can exist independently of the parent object.
b) The child object is shared between multiple parent objects.
c) The child object's lifecycle is managed by the parent object and it is destroyed when the parent object is
destroyed.
d) The relationship between parent and child objects is weak, and they can exist independently of one another.
e) Composition implies that the child object will never be destroyed, regardless of the parent object's lifecycle.
Correct Answer: c) The child object's lifecycle is managed by the parent object and it is destroyed when
the parent object is destroyed.
Explanation:
c) The child object's lifecycle is managed by the parent object and it is destroyed when the parent object
is destroyed is the correct answer because composition defines a strong relationship where the parent and child
objects are tightly bound, and the child cannot exist without the parent.
Why the other options are incorrect:
a) The child object can exist independently of the parent object: This describes aggregation, not composition.
In composition, the child cannot exist without the parent.
b) The child object is shared between multiple parent objects: This is incorrect. In composition, the child
object belongs exclusively to one parent, unlike in aggregation where sharing is possible.
d) The relationship between parent and child objects is weak, and they can exist independently of one
another: This statement describes association, not composition, where the relationship is weak and objects can
exist independently.
e) Composition implies that the child object will never be destroyed, regardless of the parent object's
lifecycle: This is incorrect because in composition, when the parent is destroyed, the child is also destroyed,
meaning their lifecycles are closely tied.
In Java, message passing refers to the process of communication
between objects. In an object-oriented programming paradigm like
Java, objects interact with each other by calling methods, passing
messages that trigger behaviors or actions. These messages typically
contain the necessary data (arguments) required to perform a certain
task, and this interaction allows for modular, reusable, and well-
encapsulated code.
Key Points of Message Passing in Java:
Objects as Communicators: Objects in Java communicate by invoking
methods on other objects. This process involves one object sending a
message (method call) to another object.
Method Invocation: The message sent from one object to another is
represented as a method call. The object that receives the method call
processes the message, often returning a result.
Arguments in Message Passing: When invoking a method, the
sender can pass arguments (message parameters) to the receiver.
These parameters provide the receiver with the necessary information
to process the message.
Dynamic Binding: In cases where the actual method to be executed is
determined at runtime (for example, if an object reference points to a
subclass object), Java uses dynamic method dispatch to resolve the
method that needs to be executed.
Happy Learning!!!
You can ask your doubts, if any, through email to
[email protected].