This blog provides Q & A on various Core Java topics.
This blog is divided in two sections Part 1 and Part 2
Click Here for Core Java Objective Questions and Answers
Part1
What are pillars of OOPS?
What is UpCasting and DownCasting UpCasting?
What is Abstract class and Interface?
What is Constructor and Constructor Overloading?
What is final?
What is static?
What is a Singleton Class
Part 2
What is Exception
How to do Exception Handling and what is the need of doing Exception handling
What is Runtime and CheckedExceptions.
How to create custom Exception
What is a Thread and how to implement a Thread.
What is synchronisation
How do Threads communicate
What is CyclicBarrier, CountDownLatch and Semaphore
What are different collections explain their differences. Also explain how they work internally?
What is static and init block?
Question : What are pillars of OOPS?
Answer :
Encapsulation
A Java Class represents encapsulation. A Class is a Structure that can contains instance variables and member methods. These instance variables define the state of the Class and the member variable define the behavior of the Class. In a Class the instance variable/data/state of the Class and behavior/methods of the Class are related.
Example
Abstraction
When a Test class creates the Object of a another class lets say Animal. It is not required that the Test Class should have the Animal.java file. Test class should just have Animal.class file which is the compiled byte code of Animal.java. By having this file the Test class can create object of Animal and call methods of Animal class without knowing their actual implementation.
This hiding of business logic is called Abstration. Only method names are known but not what is inside it.
Inheritance
When a Class inherits or extends another Class it is called Inheritance. All the instance variables and methods from Super/Base are inherited in the Sub/Derived Class.
Example Animal.java
Output after running Test.java
Polymorphism means multiple forms. There are 2 type in Java .Static(Function overloading) and Dynamic(Function overriding)
Static (Function overloading) Function Overloading happens in the same class, when there are more than one methods with the same name but different number or type of parameters.
Lets see the example
As given above the AdditionUtlity class has three methods add(int,int) add(int,int,int) and add(double,double,double). The compiler does not give any compilation errors for same method names till the methods have different number of parameters or the parameters type are different.
Please note that overloading does not happen on return type. That means if 2 method have same number and type of parameters and their return type is different, the compiler will not compile the class and will give 'duplicate method error'. On calling this methods from Test class at compilation time itself the compiler decides which method needs to be called.
Dynamic(Function overriding) Function overriding happens in case of inheritance. When a Sub class which is already inheriting a method from super class implements the same method.
Lets see the example.
Ouptput
Here the Dog class is extending Animal class. Dog has reimplemented eat() method. So if a derived class implements a method which is already inherited than this is called function overriding. In the Test class we have created reference of Animal class. But the animal reference is pointing to a Dog object.
Compiler will allow us to call only methods defined in Animal class since the reference is of Animal. At Runtime the JVM will see that the actual object is of Dog and will call the eat method of Dog if it has implemented it. This is the reason Function overriding is called Runtime Polymorphism.
Question: What is UpCasting and DownCasting UpCasting
Answer :
In the above example in Test class we saw a scenario of upcasting.
Here Animal reference is poiting to a Dog Object. This is allowed by the compiler. The compiler will not compile
Because the classes Integer and Double are not related to each other. But Animal and Dog were. Dog is a Animal. That means everything inside a Animal is their in Dog. And Dog also might have something extra. So it is perfectly safe to do Upcasting.
DownCasting
DownCasting should be done only when Upcasting is done. Downcasting is converting a reference of Superclass into Sub class. So after Upcasting is done, like below
Downcasting can be done
But this will give a compile time error. But why? Because we are trying to put a Animal into Dog. Which is not possible since Animal is less than of Dog. But since we know that animal reference is actually poiting to a Dog object, we can forcefully downcast it. Like below
After doing this the compiler will not give a compilation error irrespective of if Upcasting is done before or not. But the JVM will through a ClassCastException if Upcasting is not done at runtime. Now by this dog reference we can also call methods which are only in Dog class and not in Animal class like canbeFriend();
Question: What is Abstract class and Interface
Answer :
Abstract Class Abstract is a class whose instance can not be created. A Abstract class can have abstract and non abstract that is concrete methods. A Class that extends the Abstract class needs to implement all the methods marked abstract in the Abstract class. A class is made Abstract when creating a object of such class makes no sense. Like a Shape class. A class call Sqaure, Circle which extends Shape class are more complete. A example is given below
If a method is marked abstract in a Class than the Class should be marked abstract as well.
It is OK if a abstract class has no abstract methods.
Question : What is Constructor and Constructor Overloading
Answer :
Constrctor is a method with the same name as the class and not return type. It is called when a object of Class is created. A class can have more than one constructor, and a perticular constructor will be called depending upon the parametrs passed similar to method overloading
Output
A non parameter constructor is provided by defailt by the compiler. When we define a parametrized constructor the compiler removes the default/no param constructor. So now if we want to create a instance of a Class without passing ant parameters we will have to provide a default constructor.
Question : What is final
Answer :
Final keyword in jave in used is three ways.
Final Class, Final Method and Final Variable
Lets see in more details:
Final Class When final keyword is applied to a Class it means that that class can not be extended. A class is made final when the class has no abstract method. A class marked final can not be marked abstract and vice versa.
Final Method When a method is marked final that means that method can not be overridden. A abstract method can not be final and viceversa.
Final Variable When a variable is marked final that means it can not be changed once it is initialized.
Question : What is static
Answer :
Static keyword in java is used in three ways:
Static instance Variable:
Only instance variables can be marked static and not method local variable. When a instance variable is marked static that means there is only one copy of that variable for the Class and for all objects created of that class.
Static methods:
A static methods is generally used to initialize or access static variables in a class. There is no need to create the object of the class to access the static method.
----->>>> Core Java Interview Questions & Answers Part2
Java Interview Questions for 2 to 5 years
This blog is divided in two sections Part 1 and Part 2
Click Here for Core Java Objective Questions and Answers
Part1
What are pillars of OOPS?
What is UpCasting and DownCasting UpCasting?
What is Abstract class and Interface?
What is Constructor and Constructor Overloading?
What is final?
What is static?
What is a Singleton Class
Part 2
What is Exception
How to do Exception Handling and what is the need of doing Exception handling
What is Runtime and CheckedExceptions.
How to create custom Exception
What is a Thread and how to implement a Thread.
What is synchronisation
How do Threads communicate
What is CyclicBarrier, CountDownLatch and Semaphore
What are different collections explain their differences. Also explain how they work internally?
What is static and init block?
Question : What are pillars of OOPS?
Answer :
Encapsulation
A Java Class represents encapsulation. A Class is a Structure that can contains instance variables and member methods. These instance variables define the state of the Class and the member variable define the behavior of the Class. In a Class the instance variable/data/state of the Class and behavior/methods of the Class are related.
Example
public class Animal { int age; int height; int weight; public void eat(){ } public void walk(){ } }
Abstraction
When a Test class creates the Object of a another class lets say Animal. It is not required that the Test Class should have the Animal.java file. Test class should just have Animal.class file which is the compiled byte code of Animal.java. By having this file the Test class can create object of Animal and call methods of Animal class without knowing their actual implementation.
This hiding of business logic is called Abstration. Only method names are known but not what is inside it.
Inheritance
When a Class inherits or extends another Class it is called Inheritance. All the instance variables and methods from Super/Base are inherited in the Sub/Derived Class.
Example Animal.java
public class Animal { int age; int height; int weight; public void eat(){ System.out.println("Animal eat() called"); } public void walk(){ System.out.println("Animal walk() called"); } }Dog.java
public class Dog extends Animal{ int sensetiveNose; public void canBeFriend(){ System.out.println("Dog canBeFriend called"); } }Test.java
public class Test { public static void main(String[] args) { Dog d = new Dog(); d.eat(); d.height=14; d.age=14; d.canBeFriend(); } }
Output after running Test.java
Animal eat() called Dog canBeFriend calledPolymorphism
Polymorphism means multiple forms. There are 2 type in Java .Static(Function overloading) and Dynamic(Function overriding)
Static (Function overloading) Function Overloading happens in the same class, when there are more than one methods with the same name but different number or type of parameters.
Lets see the example
public class AdditionUtlity { public int add(int i , int j){ System.out.println("int int passed"); return (i+j); } public int add(int i , int j, int k){ System.out.println("int int int passed"); return (i+j+k); } public double add(double i , double j, double k){ System.out.println("double double double passed"); return (i+j+k); } /* public long add(int i , int j){ * System.out.println("int int"); return (i+j); }*/ } public class Test { public static void main(String[] args) { AdditionUtlity additionUtlity = new AdditionUtlity(); System.out.println(additionUtlity.add(1, 2)); System.out.println(additionUtlity.add(1, 2, 3)); System.out.println(additionUtlity.add(1.6, 1.3, 3.45)); } }
As given above the AdditionUtlity class has three methods add(int,int) add(int,int,int) and add(double,double,double). The compiler does not give any compilation errors for same method names till the methods have different number of parameters or the parameters type are different.
Please note that overloading does not happen on return type. That means if 2 method have same number and type of parameters and their return type is different, the compiler will not compile the class and will give 'duplicate method error'. On calling this methods from Test class at compilation time itself the compiler decides which method needs to be called.
Dynamic(Function overriding) Function overriding happens in case of inheritance. When a Sub class which is already inheriting a method from super class implements the same method.
Lets see the example.
public class Animal { int age; int height; int weight; public void eat(){ System.out.println("Animal eat() called"); } public void walk(){ System.out.println("Animal walk() called"); } } public class Dog extends Animal{ int sensetiveNose; public void canBeFriend(){ System.out.println("Dog canBeFriend called"); } public void eat(){ System.out.println("Dog eat() called"); } } public class Test { public static void main(String[] args) { Animal animal = new Dog(); animal.eat(); } }
Ouptput
Dog eat() called
Here the Dog class is extending Animal class. Dog has reimplemented eat() method. So if a derived class implements a method which is already inherited than this is called function overriding. In the Test class we have created reference of Animal class. But the animal reference is pointing to a Dog object.
Compiler will allow us to call only methods defined in Animal class since the reference is of Animal. At Runtime the JVM will see that the actual object is of Dog and will call the eat method of Dog if it has implemented it. This is the reason Function overriding is called Runtime Polymorphism.
Question: What is UpCasting and DownCasting UpCasting
Answer :
In the above example in Test class we saw a scenario of upcasting.
Animal animal = new Dog();
Here Animal reference is poiting to a Dog Object. This is allowed by the compiler. The compiler will not compile
Integer i = new Double();
Because the classes Integer and Double are not related to each other. But Animal and Dog were. Dog is a Animal. That means everything inside a Animal is their in Dog. And Dog also might have something extra. So it is perfectly safe to do Upcasting.
DownCasting
DownCasting should be done only when Upcasting is done. Downcasting is converting a reference of Superclass into Sub class. So after Upcasting is done, like below
Animal animal = new Dog(); ----------- Upcasting
Downcasting can be done
Dog dog = animal;
But this will give a compile time error. But why? Because we are trying to put a Animal into Dog. Which is not possible since Animal is less than of Dog. But since we know that animal reference is actually poiting to a Dog object, we can forcefully downcast it. Like below
Dog dog = (Dog)animal; ----------- Downcasting
After doing this the compiler will not give a compilation error irrespective of if Upcasting is done before or not. But the JVM will through a ClassCastException if Upcasting is not done at runtime. Now by this dog reference we can also call methods which are only in Dog class and not in Animal class like canbeFriend();
Question: What is Abstract class and Interface
Answer :
Abstract Class Abstract is a class whose instance can not be created. A Abstract class can have abstract and non abstract that is concrete methods. A Class that extends the Abstract class needs to implement all the methods marked abstract in the Abstract class. A class is made Abstract when creating a object of such class makes no sense. Like a Shape class. A class call Sqaure, Circle which extends Shape class are more complete. A example is given below
public abstract class Animal { int age; int height; int weight; public abstract void talk(); public void eat(){ System.out.println("Animal eat() called"); } public void walk(){ System.out.println("Animal walk() called"); } } public class Dog extends Animal{ int sensetiveNose; public void canBeFriend(){ System.out.println("Dog canBeFriend called"); } public void eat(){ System.out.println("Dog eat() called"); } public void talk() { System.out.println("Dog talk() called"); } }
If a method is marked abstract in a Class than the Class should be marked abstract as well.
It is OK if a abstract class has no abstract methods.
Question : What is Constructor and Constructor Overloading
Answer :
Constrctor is a method with the same name as the class and not return type. It is called when a object of Class is created. A class can have more than one constructor, and a perticular constructor will be called depending upon the parametrs passed similar to method overloading
public class Animal { public Animal() { System.out.println("no param constructor called"); } public Animal(int var) { System.out.println("one param constructor called"); age = var; } int age; } public class Test { public static void main(String[] args) { Animal animal = new Animal(); Animal animal2 = new Animal(4); } }
Output
no param constructor called one param constructor called
A non parameter constructor is provided by defailt by the compiler. When we define a parametrized constructor the compiler removes the default/no param constructor. So now if we want to create a instance of a Class without passing ant parameters we will have to provide a default constructor.
Question : What is final
Answer :
Final keyword in jave in used is three ways.
Final Class, Final Method and Final Variable
Lets see in more details:
Final Class When final keyword is applied to a Class it means that that class can not be extended. A class is made final when the class has no abstract method. A class marked final can not be marked abstract and vice versa.
Final Method When a method is marked final that means that method can not be overridden. A abstract method can not be final and viceversa.
Final Variable When a variable is marked final that means it can not be changed once it is initialized.
Question : What is static
Answer :
Static keyword in java is used in three ways:
Static instance Variable:
Only instance variables can be marked static and not method local variable. When a instance variable is marked static that means there is only one copy of that variable for the Class and for all objects created of that class.
public class Animal { static boolean extinct; int age; public void eat(){ System.out.println("Animal eat() called"); } public void walk(){ System.out.println("Animal walk() called"); } } public class Test { public static void main(String[] args) { Animal.extinct=true; Animal animal = new Animal(); animal.extinct=false; } }As seen above when class Animal is loaded in JVM at the same time extinct variable is created. And this can be accessed through Class as well as Objects of a Class
Static methods:
A static methods is generally used to initialize or access static variables in a class. There is no need to create the object of the class to access the static method.
public class Animal { static boolean extinct; int age; public static boolean getExtinct(){ return extinct; } public void eat(){ System.out.println("Animal eat() called"); } public void walk(){ System.out.println("Animal walk() called"); } } public class Test { public static void main(String[] args) { System.out.println(Animal.getExtinct()); Animal.extinct=true; Animal animal = new Animal(); System.out.println(animal.getExtinct()); } }A very important point to note is that a non static instance variable like age should not be accessed in static method like getExtinct. The reason is that age will be created only when a object is created of the Class. And static methods and variables can be accessed without creating object of the Class.
----->>>> Core Java Interview Questions & Answers Part2
Java Interview Questions for 2 to 5 years