Saturday, April 18, 2020

JAVA Programming - 4.1 OOPS basics - Introduction and Encapsulation

1. What is Encapsulation?
Encapsulation is the process of wrapping data member, member functions and member classes inside a single module called class, that defines the restriction of an object and its member variables and methods.

2. Can a class be defined inside an another class?
Yes. Class can be defined inside another class, that is called inner class. This increases the degree of encapsulation. Static class defined inside another class called Nested inner class which can be accessed without outer class instance; but Non-static defined inside another class (called inner class) can only be accessed with outer class instance.

3. What is constructor and define its function?
Constructor is a method defined inside a class which has same name as class name and it has no return time; It is used to initialize instance variable of class. If the constructor defined as private we can't create object instance of a class.

4. What will be the output of the following JAVA program?
class LWD{
public static void learn(){
System.out.println("Learning");
}
public LWD(){
System.out.println("Wondering");
}
public static void main(String l[]){
learn();
new LWD();
System.out.println("Developing");
}

}
Output & Explanation:
Learning
Wondering
Developing
Here, LWD class have two static(learn() & main()) and a constructor LWD(); Program execution start with main method which calls learn() prints "Learning" and LWD() calls prints "Wondering"; main() method prints "Developing".
Note:
learn(); // Non static method call doesn't requires object instance of the class LWD.
listen(); // Static method call requires object instance of the class LWD.


5. What will be the output of the following JAVA program?
class LWD{
class Online{
public static void main(String l[]){
System.out.println("Welcome to JAVA class");
}
}
}
Output & Explanation:
compilation error due to illegal declaration of static method inside non static inner class. Static method and variable only declared inside static block, so the inner class should be the static class.
class LWD{
static class Online{
public static void main(String l[]){ System.out.println("Welcome to JAVA class");}
}}
where output is "Welcome to JAVA class".

6. How many class files created when the following java code complied?
class AP{}
class TN{}
class College{
class Student{}
}
Answer & Explanation:
4 class files, namely
class AP{} → AP.class from AP class
class TN{} → TN.class from TN class
class College{ → College.class  from Outer class College
class Student{}→ College$Student.class from Inner class Student
}

7. What is Singleton class?
A class can have only one object instance is called Singleton class. It provides a global point of access to the object.
Singleton class used in logging, caches, thread pools, configuration settings, device driver objects.


Rules to implement singleton class:
  • Constructor should be private
  • Declare a static variable object of the class
  • Declare a static method to return the instance


8. How garbage collector knows that the object is not in use and needs to be removed?
Java garbage collection is the process by which Java programs perform automatic memory management. Java programs compile to bytecode that can be run on a Java Virtual Machine. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program. Eventually, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free up memory.

9. What is wrapper class in java?
Wrapper classes helps to convert primitives into object(Autoboxing) and object into primitives(Unboxing).

Example:
boolean→Boolean
byte→Byte
char→Character
int→Integer
float→Float
long→Long
short→Short

10. What is System.out in Java?


11. Is null a keyword in java?
 Yes. It is a reserved word(keyword) for a literal values, it is similar to true and false and used to represent the empty value or points to nothing.

12. What is java static import?

In 1.5 versions, according to sun static import improves readability of the code but according to worldwide programming exports static imports creates confusion and reduces readability of the code. Hence if there is no specific requirement never recommended to use a static import.

Without static method:
public class Main {
public static void main(String[] args) {
System.out.println(Math.sqrt(4));
}
}
With static method:
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) {
System.out.println(sqrt(4));
}
}

No comments:

Post a Comment