Java OOPS Interview Questions
Java OOPS Interview Questions
OOPS
Following
picture
show
the
topics
we
would
cover
in
this
article.
What
is
the
super
class
of
every
class
in
Java?
Every class in java is a sub class of the class Object. When we create a class we inherit all the methods
and properties of Object class. Lets look at a simple example:
String
str
=
"Testing";
System.out.println(str.toString());
System.out.println(str.hashCode());
System.out.println(str.clone());
if(str
instanceof
Object){
System.out.println("I
extend
Object");//Will
be
printed
}
In the above example, toString, hashCode and clone methods for String class are inherited from Object
class and overridden.
Can
super
class
reference
variable
can
hold
an
object
of
sub
class?
Yes. Look at the example below:
Actor reference variables actor1, actor2 hold the reference of objects of sub classes of Animal, Comedian
and Hero.
Since object is super class of all classes, an Object reference variable can also hold an instance of any
class.
What
is
Polymorphism?
Refer
to
this
video(https://www.youtube.com/watch?v=t8PTatUXtpI)
for
a
clear
explanation
of
polymorphism.
Polymorphism
is
defined
as
Same
Code
giving
Different
Behavior.
Lets
look
at
an
example.
Lets
define
an
Animal
class
with
a
method
shout.
public
class
Animal
{
public
String
shout()
{
return
"Don't
Know!";
}
Lets
create
two
new
sub
classes
of
Animal
overriding
the
existing
shout
method
in
Animal.
class
Cat
extends
Animal
{
public
String
shout()
{
return
"Meow
Meow";
}
}
class
Dog
extends
Animal
{
public
String
shout()
{
return
"BOW
BOW";
}
public
void
run(){
}
}
Look
at
the
code
below.
An
instance
of
Animal
class
is
created.
shout
method
is
called.
Animal
animal1
=
new
Animal();
System.out.println(
animal1.shout());
//Don't
Know!
Look
at
the
code
below.
An
instance
of
Dog
class
is
created
and
store
in
a
reference
variable
of
type
Animal.
Animal
animal2
=
new
Dog();
//Reference
variable
type
=>
Animal
//Object
referred
to
=>
Dog
//Dog's
bark
method
is
called.
System.out.println(
animal2.shout());
//BOW
BOW
When
shout
method
is
called
on
animal2,
it
invokes
the
shout
method
in
Dog
class
(type
of
the
object
pointed
to
by
reference
variable
animal2).
Even
though
dog
has
a
method
run,
it
cannot
be
invoked
using
super
class
reference
variable.
//animal2.run();//COMPILE
ERROR
POPULAR
VIDEOS
Java
Interview
:
A
Freshers
Guide
-
Part
1:
https://www.youtube.com/watch?v=njZ48YVkei0
Java
Interview
:
A
Freshers
Guide
-
Part
2:
https://www.youtube.com/watch?v=xyXuo0y-xoU
Java
Interview
:
A
Guide
for
Experienced:
https://www.youtube.com/watch?v=0xcgzUdTO5M
Collections
Interview
Questions
1:
https://www.youtube.com/watch?v=GnR4hCvEIJQ
Collections
Interview
Questions
2:
https://www.youtube.com/watch?v=6dKGpOKAQqs
Collections
Interview
Questions
3:
https://www.youtube.com/watch?v=_JTIYhnLemA
Collections
Interview
Questions
4:
https://www.youtube.com/watch?v=ZNhT_Z8_q9s
Collections
Interview
Questions
5:
https://www.youtube.com/watch?v=W5c8uXi4qTw
interface
Interface
{
};
class
SuperClassImplementingInteface
implements
Interface
{
};
class
SubClass2
extends
SuperClassImplementingInteface
{
};
class
SomeOtherClass
{
};
Lets
consider
the
code
below.
We
create
a
few
instances
of
the
classes
declared
above.
Lets
now
run
instanceof
operator
on
the
different
instances
created
earlier.
System.out.println(subClass
instanceof
SubClass);//true
System.out.println(subClass
instanceof
SuperClass);//true
System.out.println(subClassObj
instanceof
SuperClass);//true
System.out.println(subClass2
instanceof
SuperClassImplementingInteface);//true
instanceof
can
be
used
with
interfaces
as
well.
Since
Super
Class
implements
the
interface,
below
code
prints
true.
System.out.println(subClass2
instanceof
Interface);//true
Abstract
method
can
be
declared
only
in
Abstract
Class.
In
the
example
below,
abstractMethod()
gives
a
compiler
error
because
NormalClass
is
not
abstract.
class
NormalClass{
abstract
void
abstractMethod();//COMPILER
ERROR
}
What
is
Coupling?
Coupling is a measure of how much a class is dependent on other classes. There should minimal
dependencies between classes. So, we should always aim for low coupling between classes.
What
is
Cohesion?
Cohesion
(Video
Link
-
https://www.youtube.com/watch?v=BkcQWoF5124 )
is
a
measure
of
how
related
the
responsibilities
of
a
class
are.
A
class
must
be
highly
cohesive
i.e.
its
responsibilities
(methods)
should
be
highly
related
to
one
another.
Solution
This is a better way of approaching the problem. Different classes have their own responsibilities.
class
InternetDownloader
{
public
void
downloadFromInternet()
{
}
}
class
DataParser
{
public
void
parseData()
{
}
}
class
DatabaseStorer
{
public
void
storeIntoDatabase()
{
}
}
class
DownloadAndStore
{
void
doEverything()
{
new
InternetDownloader().downloadFromInternet();
new
DataParser().parseData();
new
DatabaseStorer().storeIntoDatabase();
}
}
What
is
Encapsulation?
Encapsulation is hiding the implementation of a Class behind a well defined interface. Encapsulation
helps us to change implementation of a class without breaking other code.
Approach
2
In
this
approach,
we
make
score
as
private
and
access
value
through
get
and
set
methods.
However,
the
logic
of
adding
4
to
the
score
is
performed
in
the
main
method.
public
class
CricketScorer
{
private
int
score;
public
int
getScore()
{
return
score;
}
public
void
setScore(int
score)
{
this.score
=
score;
}
}
Lets
use
the
CricketScorer
class.
public
static
void
main(String[]
args)
{
CricketScorer
scorer
=
new
CricketScorer();
int
score
=
scorer.getScore();
scorer.setScore(score
+
4);
}
Approach
3
In
this
approach
-
For
better
encapsulation,
the
logic
of
doing
the
four
operation
also
is
moved
to
the
CricketScorer
class.
public
class
CricketScorer
{
private
int
score;
public
void
four()
{
score
+=
4;
}
}
Lets
use
the
CricketScorer
class.
Description
In
terms
of
encapsulation
Approach
3
>
Approach
2
>
Approach
1.
In
Approach
3,
the
user
of
scorer
class
does
not
even
know
that
there
is
a
variable
called
score.
Implementation
of
Scorer
can
change
without
changing
other
classes
using
Scorer.
Example
1
doIt
method
is
overloaded
in
the
below
example:
class
Foo{
public
void
doIt(int
number){
}
public
void
doIt(String
string){
}
}
Example
2
Overloading
can
also
be
done
from
a
sub
class.
class
Bar
extends
Foo{
public
void
doIt(float
number){
}
}
Lets
create
a
sub
class
of
Animal
Cat
-
overriding
the
existing
shout
method
in
Animal.
class
Cat
extends
Animal
{
public
String
bark()
{
Constructors
Constructor (Youtube Video link - https://www.youtube.com/watch?v=XrdxGT2s9tc ) is
invoked whenever we create an instance(object) of a Class. We cannot create an object without a
constructor. If we do not provide a constructor, compiler provides a default no-argument constructor.
Is
a
super
class
constructor
called
even
when
there
is
no
explicit
call
from
a
sub
class
constructor?
If a super class constructor is not explicitly called from a sub class constructor, super class (no argument)
constructor is automatically invoked (as first line) from a sub class constructor.
Consider the example below:
class
Animal
{
public
Animal()
{
System.out.println("Animal
Constructor");
}
}
class
Dog
extends
Animal
{
public
Dog()
{
System.out.println("Dog
Constructor");
}
}
class
Labrador
extends
Dog
{
public
Labrador()
{
System.out.println("Labrador
Constructor");
}
}
public
class
ConstructorExamples
{
public
static
void
main(String[]
args)
{
Labrador
labrador
=
new
Labrador();
}
}
Program
Output
Animal Constructor
Dog Constructor
Labrador Constructor
Interface
What
is
an
Interface?
An interface (YouTube video link - https://www.youtube.com/watch?v=VangB-sVNgg ) defines
a contract for responsibilities (methods) of a class.
Example
1
Class
Aeroplane
implements
Flyable
and
implements
the
abstract
method
fly().
public
class
Aeroplane
implements
Flyable{
@Override
public
void
fly()
{
System.out.println("Aeroplane
is
flying");
}
}
Example
2
public
class
Bird
implements
Flyable{
@Override
public
void
fly()
{
System.out.println("Bird
is
flying");
}
}
Interface
methods
are
by
default
public
and
abstract.
A
concrete
method
(fully
defined
method)
cannot
be
created
in
an
interface.
Consider
the
example
below:
interface
ExampleInterface1
{
//By
default
-
public
abstract.
No
other
modifier
allowed
void
method1();//method1
is
public
and
abstract
//private
void
method6();//COMPILER
ERROR!
/*//Interface
cannot
have
body
(definition)
of
a
method
//This
method,
uncommented,
gives
COMPILER
ERROR!
void
method5()
{
System.out.println("Method5");
}
*/
}
Class
implementing
SubInterface1
should
implement
both
methods
-
method3
and
method1(from
ExampleInterface1)
An interface cannot extend a class.
/*
//COMPILE
ERROR
IF
UnCommented
//Interface
cannot
extend
a
Class
interface
SubInterface2
extends
Integer{
void
method3();
}
*/
A class can implement multiple interfaces. It should implement all the method declared in all Interfaces
being implemented.
interface
ExampleInterface2
{
void
method2();
}
class
SampleImpl
implements
ExampleInterface1,ExampleInterface2{
/*
A
class
should
implement
all
the
methods
in
an
interface.
If
either
of
method1
or
method2
is
commented,
it
would
result
in
compilation
error.
*/
public
void
method2()
{
System.out.println("Sample
Implementation
for
Method2");
}
public
void
method1()
{
System.out.println("Sample
Implementation
for
Method1");
}
}
POPULAR
VIDEOS
Java
Interview
:
A
Freshers
Guide
-
Part
1:
https://www.youtube.com/watch?v=njZ48YVkei0
Java
Interview
:
A
Freshers
Guide
-
Part
2:
https://www.youtube.com/watch?v=xyXuo0y-xoU
Java
Interview
:
A
Guide
for
Experienced:
https://www.youtube.com/watch?v=0xcgzUdTO5M
Collections
Interview
Questions
1:
https://www.youtube.com/watch?v=GnR4hCvEIJQ
Collections
Interview
Questions
2:
https://www.youtube.com/watch?v=6dKGpOKAQqs
Collections
Interview
Questions
3:
https://www.youtube.com/watch?v=_JTIYhnLemA
Collections
Interview
Questions
4:
https://www.youtube.com/watch?v=ZNhT_Z8_q9s
Collections
Interview
Questions
5:
https://www.youtube.com/watch?v=W5c8uXi4qTw