Javainterview 2
Javainterview 2
What is class?
- A class in java is a collec on of data members and member func ons. A class is a user-de ned type that
describes what a certain type of object will look like. A Class is like an object constructor, or a "blueprint"
for crea ng objects.
What is object?
- A Java object is a member of a Java class. It is also called an instance of a class. Java objects have three
primary characteris cs: iden ty, state, and behavior. These characteris cs are the building blocks of any
class object and set the scene for how they are used.
What are the access modi ers in java and why they are use?
- Access modi ers are keywords that can be used to control the visibility of elds, methods, and
constructors in a class. The four access modi ers in Java are public, protected, default, and private.
Four Types of Access Modi ers
Private: We can access the private modi er only within the same class and not from outside the class.
Default: We can access the default modi er only within the same package and not from outside the
package. And also, if we do not specify any access modi er it will automa cally consider it as default.
Protected: We can access the protected modi er within the same package and also from outside the
package with the help of the child class. If we do not make the child class, we cannot access it from
outside the package. So inheritance is a must for accessing it from outside the package.
Public: We can access the public modi er from anywhere. We can access public modi ers from within
the class as well as from outside the class and also within the package and outside the package.
What is interface ?
- The interface in Java is a mechanism to achieve abstrac on. There can be only abstract methods in the
Java interface, not method body. It is used to achieve abstrac on, mul ple inheritance and to achieve
loose Coupling in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have a
method body.
Java Interface also represents the IS-A rela onship.
The di erence between equals() method and equality operator (==) in Java?
- equals() method is used for checking the equality of contents between two objects as per the speci ed
business logic.
fi
ff
ff
ti
ti
ti
tf
ti
ti
ti
ti
ti
ti
ti
tt
ft
fl
ti
tf
ti
ti
tt
ti
ft
fi
== operator is used for comparing addresses (or references), i.e checks if both the objects are poin ng to
the same memory loca on.
What is a ClassLoader?
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
tt
ti
ti
fi
ff
ti
- Java Classloader is the program that belongs to JRE (Java Run me Environment). The task of
ClassLoader is to load the required classes and interfaces to the JVM when required.
How would you di eren ate between a String, StringBu er, and a StringBuilder?
- Storage area: In string, the String pool serves as the storage area. For StringBuilder and StringBu er,
heap memory is the storage area.
Mutability: A String is immutable, whereas both the StringBuilder and StringBu er are mutable.
E ciency: It is quite slow to work with a String. However, StringBuilder is the fastest in performing
opera ons. The speed of a StringBu er is more than a String and less than a StringBuilder. (For example
appending a character is fastest in StringBuilder and very slow in String because a new memory is
required for the new String with appended character.)
Thread-safe: In the case of a threaded environment, StringBuilder and StringBu er are used whereas a
String is not used. However, StringBuilder is suitable for an environment with a single thread, and a
StringBu er is suitable for mul ple threads.
What is the di erence between the ‘throw’ and ‘throws’ keyword in java?
- The ‘throw’ keyword is used to manually throw the excep on to the calling method.
And the ‘throws’ keyword is used in the func on de ni on to inform the calling method that this
method throws the excep on. So if you are calling, then you have to handle the excep on.
Object
Class
Inheritance
Polymorphism
Abstrac on
Encapsula on
Coupling
Cohesion
Associa on
Aggrega on
Composi on
ti
ti
ti
ti
ti