OOP Questions&Answers
OOP Questions&Answers
OOP Questions&Answers
There are many differences between object and class. A list of differences between object and
class are given below:
Class is a blueprint or
1) Object is an instance of a class. template from which objects
are created.
Object is a real world entity such as pen, laptop, mobile, bed, Class is a group of similar
2)
keyboard, mouse, chair etc. objects.
There are many ways to create object in java such as new There is only one way to
7) keyword, newInstance() method, clone() method, factory define class in java using class
method and deserialization. keyword.
Let's see some real life example of class and object in java to understand the difference well:
This is what I have heard a lot of Java programmers in my early days of learning Java say.
The very essential core of Java is that it’s designed to be an object oriented language. Let’s
look at the basic (and ritualistic) “hello world” program :
4. System.out.println("Hello, World");
5. }
6. }
This requires you to create a class as a container for the program to start execution through
the main method. Everything else can be part of this class or from a few to large number of
related classes that follow the OOP concepts like encapsulation, inheritance, composition,
polymorphism etc.
However, the reason it is not completely Object oriented is because of the presence of types
boolean , byte , char , short , int , long , float and double. These are not objects and are
collectively called as Primitives.
It’s because of these primitives that Java is not 100% OO as they can be used to hold data and
manipulate them without creating objects for them. The probable reason that they exist is
because it’s convenient (and probably faster) to use such primitive data structures
1. What is platform?
2. How C/C++ program is executed?
3. Difference between byte code and native code?
4. How Java program is executed?
Now, platform is combination of processor and OS(operating system). In general i can say
the hardware or software component in which programs run.
When you write program in C/C++ and when you compile it, it is directly converted into
machine readable language(.exe). This .exe file generated is specific to the operating system
i.e, when you compile program in windows OS, the .exe file generated for that program is
specific to only windows OS and cannot be made to run in UNIX OS.
Thats why C/C++ programs are platform dependent. Here .exe file is the Native code.
Native code is similar to machine code i.e codes that is understood by machine. Native codes
are specific to platform i.e, Native code generated by program for Windows OS is different
from Native code generated for the same program for Unix OS.
Byte codes are nothing but intermediate codes generated after compilation and it is not the
executable code like Native code. The Byte code requires a virtual machine to execute in
machine. Byte codes generated by one platform can be executed in another platform also.
When you write program in JAVA and when you compile it, a separate file is created for the
program compiled, this file(.class) is known as byte code of java. This byte code will not be
in executable stage. The main purpose of generating byte code for a program compiled is to
achieve platform independency that means this byte code generated in one platform can be
executed in another. The one which makes the byte code generated in Windows OS to be
executed in the UNIX OS is the JVM of UNIX platform. From this statement, you may have
understand that JVM is platform dependent and the byte code generated by Java program is
platform independent. The Byte code generated can run on any JVM irrespective of to which
platform the JVM belongs .Whatever the JVM in which the byte code runs the output remains
same.
To create a mutable class in Java you have to make sure the following requirements are
satisfied:
To create an immutable class in Java you have to make sure the following requirements
are satisfied:
A mutable object can be changed after it's created, and an immutable object can't.
IntegerPair(int x, int y) {
this.x = x;
this.y = y;
}
}
p.x = 50;
// p.x = 50, p.y = 10
There's no way to make existing objects immutable. Even if an object is declared final, its
fields can still be changed:
IntegerPair(int x, int y) {
this.x = x;
this.y = y;
}
}
p.x = 50;
// p.x = 50, p.y = 10
That said, if you're defining your own class, you can make its objects immutable by making
all fields final and private.
IntegerPair(int x, int y) {
this.x = x;
this.y = y;
}
}
p.x = 50;
// Compilation error: cannot assign a value to a final variable
Any time you change a string (e.g.: tacking on an extra character, making it lowercase,
swapping two characters), you're actually creating a new and separate copy:
An immutable object is a kind of object whose state cannot be modified once it is created and
mutable object can be modified after it’s creation.
We all know all the wrapper classes are immutable(Boolean, Byte, Character, Double, Float,
Integer, Long, Short, and String)
For example,
After creating String object if we try to modify it,every modification in String creates a new
String object.
System.out.println(str);
System.out.println(str);
}
O/P:
JAVA
JAVA
toLower() constructs the new string object “java” instead modifying the existed one as it is
immutable
System.out.println(str);
System.out.println(str1);
O/P:
JAVA
java
So, Every modification on immutable object creates a new object rather than modifying the
existing one ,where as in mutable the existing object gets modified.
If you want your class as immutable ,make it as final and don’t provide any methods to
modify the fields(no setter methods)