> >
Part 2-Core Java Interview Questions and Answers (Polymorphism, Overloading and Overriding)
What is Encapsulation?
It means binding the data with the code that manipulates it, it keeps the data and the code safe from external interference
By Encapsulation we can keep all the related members (variables and methods) together in an object.
The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class. However if we setup public getter and setter methods to update (for e.g. void setUserName(String userName))and read (for e.g. int getUserName()) the private data fields then the outside class can access those private data fields via public methods.
This way data can only be accessed by public methods thus making the private fields and their implementation hidden for outside classes. That’s why encapsulation is also known as '
Data Hiding' or 'Information Hiding'.
The idea of encapsulation is to keep classes separated and prevent them from having tightly coupled with each other.
Encapsulated code should have following characteristics:
- Everyone knows how to access it.
- Can be easily used regardless of implementation details.
- There shouldn’t any side effects of the code, to the rest of the application.
Difference between Abstraction and Encapsulation in Java?
Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. Abstraction is the concept of hiding irrelevant details.In other words make complex system simple by hiding the unnecessary detail from the user.
Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using private, package-private and protected access modifier.
What's the difference between "==" and "equals()"?
Both equals() and "==" operator in Java is used to compare two objects to check whether they are equal or not. The main difference between equals method and the == operator is that former is a method and later is an operator.
Since operator overloading is not supported by Java, that's why== behaves identical for every object, but user override equals() method and write their own logic to compare the objects based on the business rules.
Also Read: HashCode & equals() Interview Questions in Java
== is used to compare both primitive and objects, however equals() is only used for objects comparison.
A == B does object reference matching if both A and B are an object and only return true if both are pointing to the same object in the heap space, on the other hand, A.equals(B) is used for logical mapping and its expected from an object to override this method to provide logical equality.
What are three types of loops in Java?
Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of loops in Java:
- For loops are used in java to execute statements repeatedly for a given number of times. For loops are used when number of times to execute the statements is known to programmer.
- While loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops, condition is checked first before execution of statements.
- Do While loop is same as While loop with only difference that condition is checked after execution of block of statements. Hence in case of do while loop, statements are executed at least once.
What is an infinite Loop? How infinite loop is declared?
An infinite loop runs without any condition and runs infinitely. An infinite loop can be broken by defining any breaking logic in the body of the statement blocks. e.g:
for (;;)
{
//perform operations
..
..
//Add the loop breaking logic
if(loopBreaker)
{
break;
}
}
What is the difference between continue and break statement?
When a break keyword is used in a loop, loop is broken instantly while when continue keyword is used, current iteration is broken and loop continues with next iteration.
What is the difference between double and float variables in Java?
float takes 4 bytes in memory while Double takes 8 bytes in memory in Java. Float is single precision floating point decimal number while Double is double precision decimal number.
What is ternary operator?
Ternary operator , also called conditional operator is used to decide which value to assign to a variable based on a Boolean value evaluation. It’s denoted as ?
e.g, in the below example isAdult will be set to true if the age is greater than or equal to 18, else it will be set as false.
boolean isAdult=(age>=18) ? true : false;
Does Importing a package imports its sub-packages as well in Java?
In java, when a package is imported, its sub-packages aren’t imported and if required we need to import them separately.
Can you call a abstract method from a non abstract method ?
Yes, we can call a abstract method from a Non abstract method in a Java abstract class
What is an Inner class?
Inner class are defined inside the body of another class (known as outer class). These classes can have access modifier or even can be marked as abstract and final.
All the Inner classes have special relationship with outer class instances. This relationship allows them to have access to outer class members including private members too.
There are four types of Inner classes:
1) Inner class
2) Method-local inner class
3) Anonymous inner class
4) Static nested class
What is the difference between an Inner Class and a Sub-Class?
An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.
A sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class.
How can we copy Date object into another date Object without using a reference?
e.g:
Date mydate = new Date();
Date copiedmydate = new Date(mydate.getTime());
With Java 8 you can use the following null-safe code.
Optional.ofNullable(mydate)
.map(Date::getTime)
.map(Date::new)
.orElse(null);
ALSO CHECK: Core Java Interview Questions And Answers
-K Himaanshu Shuklaa..