Java Interview Questions : Series 2
12. What is the difference between
byte and char data types in Java?
Both byte and char are numeric data types in Java. They are used to
represent numbers in a specific range.
Major difference between them is that a byte can store raw binary
data where as a char stores characters or text data.
Usage of char is E.g. char ch = ‘x’;
Byte values range from -128 to 127.
A byte is made of 8 bits. But a char is made of 16 bits. So it is
equivalent to 2 bytes.
13. What are the main principles of
Object Oriented Programming?
Main principles of Object Oriented Programming (OOPS) are:
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
14. What is the difference between
Object Oriented Programming
language and Object Based
Programming language?
Object Oriented Programming languages like Java and C++ follow
concepts of OOPS like- Encapsulation, Abstraction, Polymorphism
and Inheritance etc.
Object Based Programming languages follow some features of
OOPS but they do not provide support for Polymorphism and
Inheritance. Egg. JavaScript, VBScript etc.
Object Based Programming languages provide support for Objects
and you can build objects from constructor. They languages also
support Encapsulation. These are also known as Prototype-oriented
languages.
15. In Java what is the default value of
an object reference defined as an
instance variable in an Object?
All the instance variable object references in Java are null.
16. Why do we need constructor in
Java?
Java is an object-oriented language, in which we create and use
objects. A constructor is a piece of code similar to a method. It is
used to create an object and set the initial state of the object.
A constructor is a special function that has same name as class
name.
Without a constructor, there is no other way to create an object.
By default, Java provides a default constructor for every object. If
we overload a constructor then we have to implement default
constructor.
17. Why do we need default
constructor in Java classes?
Default constructor is the no-argument constructor that is
automatically generated by Java if no other constructor is defined.
Java specification says that it will provide a default constructor if
there is no overloaded constructor in a class. But it does not say
anything about the scenario in which we write an overloaded
constructor in a class.
We need at least one constructor to create an object, that’s why Java
provides a default constructor.
When we have overloaded constructor, then Java assumes that we
want some custom treatment in our code. Due to which it does not
provide default constructor. But it needs default constructor as per
the specification. So it gives error.
18. What is the value returned by
Constructor in Java?
When we call a constructor in Java, it returns the object created by
it. That is how we create new objects in Java.
19. Can we inherit a Constructor?
No, Java does not support inheritance of constructor.
20. Why constructors cannot be final,
static, or abstract in Java?
If we set a method as final it means we do not want any class to
override it. But the constructor (as per Java Language
Specification) cannot be overridden. So there is no use of marking it
final.
If we set a method as abstract it means that it has no body and it
should be implemented in a child class. But the constructor is called
implicitly when the new keyword is used. Therefore it needs a
body.
If we set a method as static it means that it belongs to the class, but
not a particular object. The constructor is always called to initialize
an object. Therefore, there is no use of marking constructor static.
Inheritance
21. What is the purpose of ‘this’
keyword in java?
In Java, ‘this’ keyword refers to current instance of the object.
It is useful for differentiating between instance variables and local
variables.
It can be used to call constructors. Or it can be used to refer to the
instance.
In case of method overriding, this is used for falling the method of
current class.
22. Explain the concept of
Inheritance?
Inheritance is an important concept in Object Oriented
Programming. Some objects share certain characteristics and
behavior. By using Inheritance, we can put the common behavior
and characteristics in a base class which also known as super class.
And then all the objects with common behavior inherit from this
base class.
It is also represented by IS-A relationship.
Inheritance promotes, code reuse, method overriding and polymorphism.
23. Which class in Java is superclass
of every other class?
Java is an object oriented programming language. In Java, Object
class is the superclass of every other class.
24. Why Java does not support
multiple inheritance?
Multiple Inheritance means that a class can inherit behavior from
two or more parent classes.
The issue with Multiple Inheritance is that both the parent classes
may have different implementation for the same method. So they
have different ways of doing the same thing. Now which
implementation should the child class choose?
This leads to ambiguity in Multiple Inheritance. This is the main
reason for Java not supporting Multiple Inheritance in
implementation.
Lets say you have a class TV and another class AtomBomb. Both
have method switchOn() but only TV has switchOff() method. If
your class inherits from both these classes then you have an issue
that you can switchOn() both parents, but switchOff will only
switchOff() TV.
But you can implement multiple interfaces in Java.
25. In OOPS, what is meant by
composition?
Composition is also known as “has-a” relationship. In composition,
“has-a” relation relates two classes. E.g. Class Car has a steering
wheel.
If a class holds the instance of another class, then it is called
composition.
26. How aggregation and composition
are different concepts?
In OOPS, Aggregation and Composition are the types of association
relations. A composition is a strong relationship. If the composite
object is destroyed, then all its parts are destroyed. E.g. A Car has a
Steering Wheel. If Car object is destroyed, then there is no meaning
of Steering Wheel.
In Aggregation, the relationship is weaker than Composition.
E.g. A Library has students. If a Library is destroyed, Students still
exist. So Library and Student are related by Aggregation. A Library
has Books. If Library is destroyed, the Books are also destroyed.
Books of a Library cannot exist without the Library. So Book and
Library are related by Composition.

Comments
Post a Comment