Object Oriented Programming in Java Questions and Answers: 1. What Is Oops?
Object Oriented Programming in Java Questions and Answers: 1. What Is Oops?
Answers
1. What is OOPs?
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
[www.tutorialsfield.com] Page 1
3. Why use OOPs?
• Code maintenance
• Reusability
• Security
• Better productivity
• Polymorphism flexibility
• Problem solving
• Easy troubleshooting
• Design benefits
• Data redundancy
[www.tutorialsfield.com] Page 2
OOP SOP
5. What is a class?
A class is a collection of objects. Classes don’t consume
any space in the memory.
It is a user defined data type that act as a template for
creating objects of the identical type.
[www.tutorialsfield.com] Page 3
Once a class has been defined. A large number of objects
can be created using the same class. Therefore, Class is
considered as the blueprint for the object.
For eg. Fruit is a class and its objects are mango, apple
etc. Furniture is a class and its objects are table, chair,
desk etc.
Syntax :
class classname [extends inheritance]
{
[field declararion]
[method declaration]
}
6.What is an object?
An object is a real world entity which have properties and
functionalities.
Object is also called an instance of class. Objects take
some space in memory.
For eg. car, table, chair etc. are the example of objects.
Syntax of creation of object :
[www.tutorialsfield.com] Page 4
classname obj_reference = new classname();
OR
Class Object
[www.tutorialsfield.com] Page 5
5. Eg. - Fruit, Vehicle, Laptop Eg. - Mango, Car, HP Laptop
Class Structure
Structure is a collection of
1. Class is a collection of
variables of different data
objects.
types under a single unit
6. Classes are ideal for larger or Structures are ideal for small
complex objects and isolated model objects
[www.tutorialsfield.com] Page 6
9. What is encapsulation?
Encapsulation is an striking feature of OOPs that provides
data security. It is a mechanism of binding member data
and member function together into a single place i.e. class.
The main advantage of encapsulation is that data is hidden
and protected from access by outside non-member
methods of a class. In other words, only member
functions defined in a class will have access to the data.
In encapsulation, data(variables) are declared as private
and methods are declared as public.
[www.tutorialsfield.com] Page 8
Accessible Accessible
Accessible Accessible by
by sub- by sub-
Access by classes in classes in
classes in classes in
Modifiers the same other
the same other
package packages
package packages
Private No No No No
[www.tutorialsfield.com] Page 9
and only the necessary functionality is shown or provided
to the user.
[www.tutorialsfield.com] Page 10
The member of the class may or may not be private but
the members of the interface can never be private.
The abstract method of the interface must be overridden in
its child classes and the interface supports the concept of
multiple inheritance which was not supported using the
classes.
An interface can't be extended by class, it can only be
implemented by class.
Syntax :
interface interfacename
{
[final field declaration]
[abstract method declaration]
}
[www.tutorialsfield.com] Page 11
Data Abstraction Encapsulation
3. Data abstraction is
Encapsulation is implemented
implemented at the design or
at the implementation level.
interface level
[www.tutorialsfield.com] Page 12
class to a new class. In other words, A class inherits data
fields or methods from another class.
For eg. Animal is a class and other classes like Dog, cat
etc. can inherit the common properties of animal class.
This helps to write redundant free code and also reduces
code length.
[www.tutorialsfield.com] Page 13
Sub class is that class which inherits other class. In other
words, sub class is a class which inherits the properties
and behaviors of super class. It is also called child class or
derived class.
[www.tutorialsfield.com] Page 14
22. What is multilevel
vel inheritance?
When a class inherits
rits other class and then that
th inherited
class further inherite
erited by other class, this
th type of
inheritance is called
dmmultilevel inheritance.
In multilevel inheritance
itance, sub class contains
s the properties
of the base class as wwell as properties of base
ase class of its
own base class.
[www.tutorialsfield.com]
m] Page 15
23. What is multiple
le in
inheritance?
When a class inherits
its tthe properties and behavi
haviors of more
than one class thenhen this type of inheritance
tance is called
multiple inheritance.
In multiple inheritance
tance, there is only one sub
su class but
more than one superer cl
class.
Java doesn't support
rt m
multiple inheritance.
[www.tutorialsfield.com]
m] Page 16
24. What is hierarchica
chical inheritance?
When more than on one class inherits the properties
pr or
behavior of only onene cl
class then this type off inheritance
in is
called hierarchical inhe
inheritance.
In hierarchical inherita
eritance, there are only one
ne parent
p class
but many child class.
[www.tutorialsfield.com]
m] Page 17
25. What is hybrid inhe
inheritance?
Hybrid inheritance is a combination of more than
tha one type
of inheritance.
[www.tutorialsfield.com]
m] Page 18
26. Why Java doesn't support multiple inheritance?
Java doesn't support multiple inheritance because of
following reasons -
Ambiguity Around The Diamond Problem
Multiple inheritance does complicate the design and
creates problem during casting, constructor chaining etc.
So to simplify the language and reduce code complexity,
Java doesn't support multiple inheritance.
[www.tutorialsfield.com] Page 19
class. and derived class.
[www.tutorialsfield.com] Page 20
on the type of parameters, order of parameters, and
number of parameters.
In simple words, Polymorphism is the ability of an object
to take many forms.
[www.tutorialsfield.com] Page 21
The polymorphism that takes place during run time is
called compile time polymorphism. It is also called
dynamic polymorphism or late binding.
Runtime polymorphism refers to the process when a call to
an overridden process is resolved at the run time.
In this type of polymorphism, the sub class and base class
both contain methods having same name which can have
different functionalities.
[www.tutorialsfield.com] Page 22
When base class contain a method and sub class also
contain same name method of its parent class, this is
called method overriding.
In other words, base class and subclass both have same
name methods as well as signatures too.
Method overriding is an example of run time
polymorphism.
Method overriding is used to provide the specific
implementation of a method which is already provided by
its superclass.
[www.tutorialsfield.com] Page 23
Static methods can not be overridden. They are stored in
heap space of the memory.
[www.tutorialsfield.com] Page 24
39. What is Constructor?
Constructor is a special type of member function which is
used to initialize an object. It is similar as functions but it's
name should be same as its class name and must have no
explicit return type.
It is called when an object of the class is created. At the
time of calling constructor, memory for the object is
allocated in the memory.
We use constructor to assign values to the class variables
at the time of object creation.
[www.tutorialsfield.com] Page 25
41. What is default constructor?
A constructor with 0 parameters is known as default
constructor.
[www.tutorialsfield.com] Page 26
45. What is private constructor?
Java enables us to declare a constructor as private. We
can declare a constructor private by using
the private access specifier. Note that if a constructor is
declared private, we cannot create an object of the class.
Instead, we can use this private constructor in Singleton
Design Pattern.
[www.tutorialsfield.com] Page 27