OOP
Ques 1: What is a class?
Ans. Class is a blueprint or template for creating an object.
it defines properties and behaviours of object.
Ques 2: What is the need for OOPs?
Ans. OOPs is needed to organise the code, reuse it, secure data and model real-world entity
effectively.
Without OOPs, large projects difficult to manage, extends, and secure. OOPs slove these
problems efficiently.
Ques 3: What is an object?
Ans. An Object is a real-world entity in java it has states and behaviour. It is an instance of class.
Ques 4: Define a constructor.
Ans. Constructor is a special method in java that has the same name as class and doesn’t have
return type. It is used to initialize state of objects.
Ques 5: Define Destructor.
Ans. In java there is no explicit destructor.
Object cleanup is handled automatically by the Grabage collector, which destroys the
objects that are no longer in use.
Java use Garbage Collector instead of destructor for object clean up.
Ques 6: What is the difference between multiple and multilevel inheritance?
Ans. Multiple Inheritance – A class inherits from more than one parent class.
It is not supported using classes (avoid ambiguity) but possible using interface.
Multilevel Inheritance – A class inherits from another class, which itself inherits from
another class (chain of Inheritance).
Ques 7: What is a superclass?
Ans. Superclass (base class) is a class that act as a parent class to one or more classes. Allowing
them to inherits properties and methods.
Ques 8: Differentiate between overloading and overriding.
Ans. Overloading – Method Overloading allows multiple methods in the same class with the same
name and different parameters. The return type can be same or different. it also called
compile time polymorphism
Overriding – Method Overriding allows a child class to provide its own implementation of a
methods from the parent class. The return type must be same. It has “is-a” relationship and
also called Runtime polymorphism.
Ques 9: What is a friend function?
Ans. A friend function is a function that is not a member of class but can access the private and
protected member of that class.
Java doesn’t support friend function.
Friend function sexist in c++ only.
Java doesn’t have friend function, access is controlled by access modifiers (private, public,
protected).
Ques 10: What is a virtual function?
Ans. A virtual function is a function in a base class that can be overridden in a derived class to
achieve runtime polymorphism.
In java, all non-static variable or non-private methods are virtual by default.
Explicit virtual keyword is used in c++ not in java.