Imprtant Questions For Exams
Imprtant Questions For Exams
Inheritance
Encapsulation
Polymorphism
Abstraction
What is Encapsulation?
Encapsulation is also a part of the OOPs concept. It refers to the bundling of data with the
methods that operate on that data. It also helps to restrict any direct access to some of an
object‟s components.
What is Abstraction?
Abstraction is an OOPs concept to build the structure of real-world objects. It “shows” only
essential attributes and “hides” unnecessary information from the outside. The main focus
of abstraction is to hide unnecessary details from the users. It is one of the most important
concepts of OOPs.
Single Inheritance
Multiple Inheritance
Multi-level Inheritance
Hierarchical Inheritance
Hybrid Inheritance
In OOP, you combine the code into one unit so you can specify the parameters of each piece
of data. This process of wrapping up data into a single unit is called encapsulation.
By using classes, you can generalise your object types and make your application easier to
use. This is termed as an abstraction.
The ability for a class to inherit characteristics and behaviours from another class allows for
more code reuse.
Polymorphism allows for the creation of several objects from a single, adaptable class of
code.
Is it possible to call the base class method without creating an
instance?
Yes, we can possibly call the base class method without creating an instance in the following
3 cases:
Types of constructor
Types of constructors depend upon languages
Private Constructor
Default Constructor
Copy Constructor
Static Constructor
Parameterized Constructor
What are accessors ?
- They are the functions in the public interface that provide accessibility to the private data
members for “read” operations.
- Example: see the getX, getY functions in the Point class.
A user-defined class serves layout or blueprint from which objects can be built. In essence, a
class is made up of fields known as attributes and methods known as member functions that
define actions. A structure is a grouping of variables of various data kinds under one
heading.
What is inheritance?
Whenever one class is derived from another, it is referred to as inheritance. The child class
will inherit all of the parent class‟s public and protected properties and methods. Apart from
the attributes and methods inherited from the parent class, it can also have its own additional
set of features. The‟ extends‟ keyword is used to specify an inherited class.
If you derive a class from another class that is known as inheritance. The child class
will inherit all the public and protected properties and methods from the parent class. The
child class can also have its own properties and methods. An inherited class is defined by
using the extends keyword.
A super class or base class is also a class that works as a parent to some other class/
classes.
What is a subclass?
A class that derives from another class is referred to as a subclass. A subclass inherits the
properties of its ancestors or parent classes. For example, the class Bike is a subclass or a
derivative of the Vehicle class.
What is Polymorphism?
Polymorphism is one of the most used and core concepts in OOP languages. It explains
the concept of different classes can be used with the same interface. Each of these classes
can have its own implementation of the interface.
The user-defined data type is given a special meaning by the operator using operator
overloading. It is a compile-time polymorphism.
What is encapsulation?
Encapsulation is used to wrap the data and the code which works in a single unit together.
Example: Encapsulation allows data-hiding as the data specified in one class is hidden from
other classes.
For example, while using a mobile, you know, how can you message or call someone but
you don‟t know how it actually happens.
This is data abstraction as the implementation details are hidden from the user.
How to achieve data abstraction?
Data abstraction can be achieved using two ways:
Abstract class
Abstract method
What is an abstract class?
An abstract class is also a class which is consists of abstract methods.
These methods are basically declared but not defined and If these methods need to be used
later in some subclass that time those methods have to be exclusively defined in the
subclass.
What is a destructor?
A destructor is a method that is called automatically when an object is destroyed.
The destructor also recovers the heap space which was allocated to the destroyed object. It
also start closing the files and database connections of the object, etc.
What is an exception?
An exception is a kind of message that interrupts and comes up when there is an issue with
the normal execution of a program. Exceptions provide an error and transfer it to the
exception handler to resolve it. The state of the program is saved as soon as an exception is
raised.
Can you call the base class method without creating an instance?
Yes, you are allowed to call the base class without instantiating it but there are some
conditions that are applicable:
If it is a static method
The base class is inherited by some other subclass
Ex: -
1. An Animal is a class, and cat, dog, etc., are objects with common properties like name, type, and common
behaviors like speaking, walking, running, etc.
2. Mobile is a class, and Nokia, moto, iPhone, etc., are objects with common properties like modal_no, color,
etc., and common behaviors like audio_calling, video_calling, music, etc.
The interface should be used if just the requirement specification is known and nothing about
implementation. If the implementation is known, but partially, then an abstract class should
be used. If the implementation is known completely, then a concrete Class should be used.
1. Using the “this” keyword, the reference can be made to the constructor in the current class.
2. To call the constructor from the base class “super” keyword will be used.
Note: Only existing operators in C++ can be overloaded and the overloaded operator must
have an operand of the user-defined type.
Variables should be declared before using them in Java programming. Variable initialization
can be static or dynamic. The syntax for variable declaration and static initialization is: –
Types of variables
Primitive Variables: It is used to represent primitive values like int, float, etc.
Instance Variables: Variables whose value varied from object to object are instance
variables. For every object, a separate copy of the instance variable is created. Instance
variables are declared within the Class and outside any method/block/constructor
Static variables: For static Variables, a single copy of the variable is created, and that copy
is shared between every Class object. The static variable is created during class loading and
destroyed at class unloading.
Static variables can be accessed directly from the static and instance area. We are not
required to perform initialization explicitly for static variables, and JVM will provide default
values.
Local Variables: Variables declared inside a method or block or constructor are local
variables. Hence the scope of local variables is the same as the block‟s scope in which we
declared that variable.