IPSR Solutions ltd

IPSR - Blog

Our blog gives you with the latest updates from the IT industry such as
networking, software and new technologies. We look forward for active discussions.

Selected Java Interview Questions Part – 2

javainterview

8)What is method overriding?

If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method.

Eg:

class Vehicle{
void run(){System.out.println(“Vehicle is running”); }}
class Bike2 extends Vehicle{
void run(){System.out.println(“Bike is running safely”);}
public static void main(String args[]){
Bike2 obj = new Bike2();
obj.run();
}

Output:

Bike is running safely

9) Difference between method Overloading and Overriding.

Method Overloading:

1)    Method overloading increases the readability of the program.
2)    method overlaoding is occurs within the class.
3)    In this case, parameter must be different.

Method Overriding:

1)    Method overriding provides the specific implementation of the method that is already provided by its super class.
2)    Method overriding occurs in two classes that have IS-A relationship
3)    In this case, parameter must be same.

10) What is Runtime Polymorphism?

Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.

In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.

Eg:

class Bike{
void run(){System.out.println(“running”); }}
class Splender extends Bike{
void run(){System.out.println(“running safely with 60km”);}
public static void main(String args[]){
Bike b = new Splender();//upcasting
b.run();
}}

Output:

Running safely with 60km

11) What is the difference between static binding and dynamic binding?

In case of static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at runtime.

Eg for static binding:

class Dog{
private void eat(){System.out.println(“dog is eating…”);}
public static void main(String args[]){
Dog d1=new Dog();
d1.eat();
}
}

Eg for dynamic binding:

class Animal{
void eat(){System.out.println(“animal is eating…”); }}
class Dog extends Animal{
void eat(){System.out.println(“dog is eating…”);}
public static void main(String args[]){
Animal a=new Dog();
a.eat();
}

Output:

dog is eating…

12) What is abstraction?

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Eg:

abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println(“running safely..”);}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}}

Output:

Running safely

13) What is the difference between abstraction and encapsulation?

Abstraction hides the implementation details whereas encapsulation wraps code and data into a single unit.

14) What is interface?

Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully abstraction and multiple inheritance.

Eg:

interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println(“Hello”);}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}

Output:

Hello

15) What is difference between abstract class and interface?

Abstract class:

1)  An abstract class can have method body (non-abstract methods).
2)  An abstract class can have instance variables.
3)  An abstract class can have constructor.
4)  An abstract class can have static methods
5)  You can extends one abstract class.

Interface:

1)    Interface have only abstract methods.
2)    An interface cannot have instance variables.
3)    Interface cannot have constructor.
4)    Interface cannot have static methods.
5)    You can implement multiple interfaces.

For IPSR MEDIA:
Rudit Mathews | Java + Android Student ( IT FINISHING SCHOOL) | IPSR INTERNATIONAL CENTRE, KOCHI ( Edappally)
Share Button

Leave a Reply

Archives

Menu Title