Gadgets

Saturday, 27 February 2016

OOPs Interview questions

Java is Object Oriented Programming language, in this page I given interview questions on OOPs concept. The OOPs concepts are very basic concepts in java.
1) Why java is Object Oriented Programming language?
Java is object based, it is consider the data in object form. Here we have some topics to say java is Object Oriented Programming language.
·        Abstraction
·        Encapsulation
·        Inheritance
·        Polymorphism etc.

2) What is the Data hiding?
If we hide the data internally and other programmers don’t have permission to access our internal data is known as data hiding.

3) What is the Abstraction?
Hiding the internal implementation details and showing the service (functionality) is known as abstraction. Example: bank ATM, Phone call.
We don’t know the ATM internal implementation but we use ATM services.

4) What is the Encapsulation?
Binding code into a single unit is known as Encapsulation.

5) What is IS-A relationship?
IS-A relationship is known as Inheritance, we can implement IS-A relationship by using extends keyword. We can inherit all the property from parent to child class using IS-A relationship.
class One {
}
class Two extends One {
            // inherit all parent class properties
}

6) What is HAS-A relationship?
We can implement HAS-A relationship using new keyword, using HAS-A relationship access specific functionalities from parent to child class.
class One {
}
class Two extends One {
             One obj=new One();
            obj.methods();// required functionalities
}

7) What is the method signature?
Method signature consist method name followed by argument types
public static int add(int number1, int number2)
add(int number1, int number2)// is method signature
8) What is the method overloading?
If two methods contain same method name, same return type and different arguments is known as method over loading.
class Test {
public void display(int number) {            
}
public void display(double number) {            
}
}

9) What is the method overriding?
If two methods contain same method name and same parameters then it is method overriding.
10) What is method hiding?
If a subclass defines static method with same name, same parameters and same return type as same in the super class then it is method hiding.
public class Test {
            public static void main(String[] args)            {
                        System.out.println("parent");
            }
}
class Exam extends Test {
public static void main(String[] args) {
                        System.out.println("child");
            }
}
/* OUTPUT :
D:\Java>java Test
parent
D:\Java>java Exam
child   */

11) Is it possible to define multiple inheritance in java?
A java class can’t extends more than one class at a time hence multiple inheritance is not applicable in java.
class One extends Two
{
}
But it is possible in interfaces. Interface can extends more than one class at time.
interface Interf extends One,Two
{
}

12) What are the various ways to create object?
There are five ways to create object in java
Using new operator:
Test obj=new Test();
Using newInstance():
Test obje=(Test)Class.forName("Test").newInstance();
Using clone():
Test obj=new Test();
Test obj2=(Test)obj.clone();
Using factory method:
Runtime r=Runtime.getRuntime();
Using Deserialization:
FileInputStream fis=new FileInputStream("file.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
Test obj=(Test)ois.readObject();

13) What is factory method?
If we call a method by using class name and that method returns the same class object then such types of method is factory method.
Runtime r=Runtime.getRuntime();

14) What is super(), this(), super and this?
super() is used to invoke super class constructors and super keyword is refer super class members.
this() is used to invoke current class constructor and this keyword is refer current class instance members.

1 comment:

  1. It is very good blog and useful for students and developer , Thanks for sharing

    Java Online Training Hyderabad

    ReplyDelete