[Go to site: main page, start]

0% found this document useful (0 votes)
7 views12 pages

Java Interview Questions

The document explains various concepts in Java, including types of constructors (default and parameterized), the static keyword, the final keyword, method overloading vs. overriding, IS-A and HAS-A relationships, and the use of 'this' and 'super' keywords. It also covers runtime polymorphism, static vs. dynamic binding, and the use of the instanceof operator. Each concept is illustrated with code snippets for clarity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Java Interview Questions

The document explains various concepts in Java, including types of constructors (default and parameterized), the static keyword, the final keyword, method overloading vs. overriding, IS-A and HAS-A relationships, and the use of 'this' and 'super' keywords. It also covers runtime polymorphism, static vs. dynamic binding, and the use of the instanceof operator. Each concept is illustrated with code snippets for clarity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

What are the different types of constructors in Java? And How to overload Constructor?

Constructor is a block of code, which will have a name as the Class name. The constructor
will be executed when a new instance of the class is created.

There are two types of Constructor

Default Constructor

Default constructor will not have any parameter, If you don’t define it then compiles creates
one automatically. Default constructor will be called by compiler if there is no other
constructor.

Example Code Snippet

class Academy {

// Constructor

Academy() {

[Link]("Constructor called");

public class SampleClass {

public static void main(String args[]) {

// this invokes default constructor.

Academy academy = new Academy();

Output:

Constructor called

Parameterized Constructor

The parameterized constructor will have parameters, this is mainly used for initialization
purposes. You can pass different values to a parameterized constructor at the time of object
creation, but values should match the constructor signature

Example Code Snippet

class Academy {
int a=0;

int b=0;

// Parameterized Constructor

Academy(int a, int b) {

[Link]("Constructor called");

[Link]("Addition of "+a+" and "+b+" = "+(a+b));

public class SampleClass {

public static void main(String args[]) {

// this invokes default constructor.

Academy academy = new Academy(10,20);

Output:

Constructor called

Addition of 10 and 20 = 30

What is the static keyword? How it can be used?

The static indicates that the particular member belongs to a type itself, rather than to an
instance of that type. That means a static member of the class can be accessed before
creating any object for that class.

Static keywords can be used for

 Variable, also called static fields or Class Variables

 Method

 Block

 Nested Class

Static Variable or Static Fields:


When you prefix fields or variables with static which is called a static variable. When you
create the static variable single copy of that variable is created and shared among all
instances of an object.

 The static variable associated with the class can be accessed directly using the class
name. The object reference is not needed.

 Static variables are created at the class level.

Example Code Snippet

public class SampleClass{

//Example of static variable

public static int exampleVariable =10;

Static method

When you create a method and prefix with a static keyword it becomes static methods,
static methods belong to the class, not to any object.

 The static method can be accessed using class name no need for any object
reference. The main method is the example of a static method which is commonly
used

 A static method cannot be overridden

 You cannot create abstract method using static keyword

 this or super keyword cannot be used for static methods

Example Code Snippet

public class SampleClass{

public static void sampleMethod(String name) {

[Link](name);

Static Block

A block of code which is used with static keyword is known as static block. Static block is
mainly used for initializing static variables. You can have multiple static block inside single
class. Static block will be executed before main method

Example Code Snippet


public class SampleClass{

public static List<String> fruits = new LinkedList<>();

static {

[Link]("Apple");

[Link]("Banana");

[Link]("Kiwi");

public static void main(String args[]) {

[Link]([Link](0));

Output: Apple

Static Class

When you create a class inside another class it is called a nested class, static class only be
created as a nested class. Members of the static class can be directly accessed using the
outer class.

 A static class cannot access non-static members of the Outer class

 Static class members can be called with outer class reference

 Static classes are always nested or inner class

 You can use access modifier for static class

Example Code Snippet

public class SampleClass{

//static class declared here

static class SampleStaticClass{

int a = 10;

public static void main(String args[]) {


[Link] staticClass = new [Link]();

[Link](staticClass.a);

What is this final keyword? how I can be used?

The final keyword is used for restricting the behavior variable, method, class, etc.

The final keyword can be used with

 Variable

 Methods

 Class

Final Variable:

 If you prefix any variable with a final keyword then it is called the final variable.

 Once the value is assigned to the final variable you cannot change it.

 If you don’t initialize the final variable during declaration it is called blank final
variable, it can be initialized through constructor.

 A final variable can be declared as a static

 You can assign the final static variable during the declaration

 If you have created a blank static final variable, it can be initialized inside the static
block.

Example Code Snippet

public class FinalTest{

public static void main(String args[]) {

final int i = 10;

Final Methods

If you prefix the final keyword to methods it becomes the final method

Final methods cannot be overridden

Static final method is completely valid in java, and it can be declared


Example Code Snippet

final void print() {

int a = 10;

[Link](a);

Final Class

If you prefix final to class, then it becomes the final class

Final classes cannot be extended or inherited

You can create the inner class as a static final

Example Code Snippet

public final class

//Some Code

What is the difference between method overloading and method overriding?

Method overloading is class has more than one method of the same name but having
different parameter. Method overriding Subclass provides the implementation of the
method that has been declared by the Parent class is called method overriding.

Method overloading is an example of compile-time polymorphism. Method overriding is an


example of run time polymorphism.

You cannot achieve overloading only by changing the return type. Method overriding will
have usually the same return type.

Method overloading can be achieved without inheritance. Method overriding requires


inheritance

Example Code Snippet for method overloading

//Method overloading

public class SampleClass{

public void add(int a, int b, int c) {

//some code

}
public void add(int a, int b) {

//some code

Example Code Snippet for Method overriding

//Method overriding

public class SampleClass{

public void someMethod() {

[Link]("Super Class");

class MyClass extends SampleClass{

public void someMethod() {

[Link]("Sub Class");

6. Could you explain IS-A and HAS-A relationship?

IS -A relationship

IS-A relationship is formed in inheritance, The class which inherit is known as subclass or
child class. That means that child class is the type of parent class.

For example, Orange is a Fruit, you need to extend fruit class to make IS-A relationship

Example Code Snippet : IS-A Relationship

class Fruit{

//some code

//IS-A relationship

class Orange extends Fruit {

//some code
}

HAS -A relationship

HAS-A relationship is composition, which means you are creating the object of a class inside
another class. Let’s take the example of Room. The room has a chair. The room should
contain chair class reference, i.e you need to create an instance of Chair particular class
inside the Room class.

Example Code Snippet : HAS-A Relationship

class Room {

Chair chair = new Chair();

Explain this and super keyword

this keyword is used to refer current class instance/object

this keyword can be used to

 Current class instance variable

 Invoke current class methods

 Invoke current class constructor

 Can be used to pass the argument to a method

 Can be used to pass the argument to a constructor

 Return the current class instance

Super Keyword

 The super keyword is a reference variable that is used to refer to parent class objects

 super can be used to refer to immediate parent class instance variable

 super can be used to invoke the parent class method

 super can be used to invoke parent class Constructor

Example Code Snippet : this and super keywords

class First {

void print() {

[Link]("Printing First...");

}
}

class Second extends First {

void printOneMore() {

[Link]("Printing Second...");

void myMethod() {

[Link]();

[Link]();

public class KeywordTest {

public static void main(String args[]) {

Second d = new Second();

[Link]();

How to check instance of Specified type? Explain with example

The Java provides instanceof operater which can be used to check instance of Specified
type.

 The instanceof operator returns either true or false

 The instanceof operators are used for down casting

 The instanceof operator works on the principle of the is-a relationship

 If you instanceof Operator for null object it will return false.

Example Code Snippet

public class Apple {

public static void main(String args[]) {

Apple apl = new Apple();


if (apl instanceof Apple) {

[Link]("apl is instance of Apple");

} else {

[Link]("apl is not an instance of Apple");

What is run time polymorphism in Java?

Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an


overridden method is resolved at runtime rather than compile-time.

 The overridden method is called through the reference variable of a superclass

 The object is determined by the object being referred to by the reference variable

 The reference variable of Parent class refers to the object of Child class is called
upcasting

class Car {

void print() {

[Link]("Inside Car Class");

class Honda extends Car {

void print() {

[Link]("Inside Honda Class");

public class MainClass {

public static void main(String args[]) {

Car car = new Honda();// This is Upcasting

[Link]();

}
}

11. What is the difference between static and dynamic binding in java?

 The binding which can be resolved by the compile-time is static binding. The binding
which will be resolved at the run time is known as dynamic binding.

 Type information is used for resolving static binding. Object information is used for
resolving dynamic binding

 Method overloading is an example of static binding. Method overriding is an example


of dynamic binding

 Static binding is also known as early binding. Dynamic binding is also known as late
binding

Example Code Snippet: Static Binding

//Static binding

public class MainClass {

void print() {

[Link]("Printing");

public static void main(String args[]) {

MainClass mc = new MainClass();

[Link]();

Example Code Snippet: Dynamic binding

//Dynamic Binding

class Fruit {

void print() {

[Link]("This is fruit class");

public class Apple extends Fruit {


void print() {

[Link]("This is Apple");

public static void main(String args[]) {

Fruit f = new Apple();

[Link]();

You might also like