Select chapter



basics (7.papers)
constructors (1.papers)
packages (1.papers)
inheritance (7.papers)
misc (3.papers)
exceptions (5.papers)
threads (4.papers)
overloading (1.papers)
jsp (3.papers)
servlets (9.papers)
jdbc (2.papers)
util (1.papers)
xml (2.papers)
IO stream (3.papers)
Hide

Sponsored Links


  

java interface abstractclass interview questions and answers

1
What is the problem with below Java program?
abstract class MyAbsClass{
        public static int count = 100;
        public static void fun(){
                System.out.println("MyAbsClass static fun");
        }
        public void test(){
                System.out.println("MyAbsClass testing");
        }
        public abstract void disp();
}
class MyDer extends MyAbsClass{
        @Override
        public void disp() {
                System.out.println("MyDer disp.."+count);
        }
}
public class AbstractDemo4 {
        public static void main(String[] args) {
                MyDer md = new MyDer();
                System.out.println(MyDer.count);
                MyDer.fun();
                md.disp();
        }
}
  1. Compile time error in main method. We can't call MyDer.count & MyDer.fun() because those are static variable and methods of MyAbsClass. They are not part of MyDer class.
  2. Compile time error in class MyDer. we can't call count from disp() method, because count is class variable of MyAbsClass, and static variables will not be inherited to derived classes.
  3. Compile time error in class MyAbsClass. Abstract class can't have static variables or static methods.
  4. 100
    MyAbsClass static fun
    MyDer disp..100
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
4
When to use abstract classes in java?
  1. If same kind of derived classes has some thing in common, and if you don't know some of the specific functionalities, then we can use abstract classes. Eg: Rectangle, Circle, Line everything is a shape. And we know that every shape will have color, x, y co-ordinates. But we don't know the specific functionalities like area, because area formula will change for specific shape.
  2. Abstract classes will be heavily used in framework layer classes, where you know how to handle generic events coming to all the applications, but you don't know what to do in startup() and shutdown() functionalities. (Because these are specific to applications)
  3. If you are providing some functionalities for some un related classes , then don't use abstract classes.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
5
Abstract class example in java:
Is there any problem with below Java program?
abstract class MyApplication{
        protected abstract void startUp();
        protected abstract void shutDown();
        public void run(){
                startUp();
                shutDown();
        }
}
  1. Compile time error. abstract classes should have only public abstract methods. Here abstract methods are declared as protected, which is not possible.
  2. Compile time error in run() function. We can't call startUp(); and shutDown() from run() method because they don't have any function body.
  3. There is no problem in this given program.
  4. all are wrong.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
7
What is the output of below Java program?
class ClientInter{
        public void done(){
                System.out.println("Client work is done");
        }
}
class Server{
        public void run(ClientInter c){
                System.out.println("Server is running..");
                c.done();
        }
}
class Client1 extends ClientInter{
        public void done(){
                System.out.println("Client 1 work is done");
        }
}
public class ServerDemo {
        public static void main(String[] args) {
                Server s = new Server();
                Client1 c = new Client1();
                s.run(c);
        }
}
  1. Compile time error in Server class run method. it is not possible to assign Client1 object to ClientInter variable.
  2. Server is running..
    Client 1 work is done
  3. Server is running..
    Client work is done
  4. Run time exception, ClassCastException in Server class run() method. We are passing Client1 object in main method and assigning it to ClientInter variable in run() method, which is not possible. Though it won't give compile time error, but when you run it, it will give run time exception.
Show Answer
Share Facebook gmail Twitter
8
What is the difference between abstract class and interface in Java?
  1. abstract class is kind of incomplete class which contains only partial implementation.Abstract class may contain 0 or more number of abstract methods. Abstract class can also contain some of method with definitions. Interface can contain only abstract methods. Interfaces can't contain method bodies.
  2. Generally we use abstract classes to share common functionality to similar derived classes and leaving some specific implementations to derived classes by making them as abstract functions in the abstract class. Interfaces will be generally used while sharing a library with third parties as a security mechanism. We can also use interfaces to achieve multiple inheritance indirectly.
  3. 1.Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can’t have any method implementations.
    2.Abstract classes can have constructors but interfaces can’t have constructors.
    
    3.Abstract class have all the features of a normal java class except that we can’t instantiate it. We can use abstract keyword to make a class abstract.
    
  4. all are true.
Show Answer
Share Facebook gmail Twitter
9
What is an abstract class in Java?
  1. abstract class is kind of incomplete class which contains only partial implementation.Abstract class may contain 0 or more number of abstract methods. Abstract class can also contain some of method with definitions.
  2. Abstract classes can't be instantiated (can't create objects for it).
  3. if a class contains at least one abstract method then it should be made as abstract class.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
10
What is abstraction in Java?
  1. Abstraction means hiding unwanted details and showing what is required.
  2. Abstraction in Object Oriented Programming helps to hide the irrelevant details of a class. Example: we hide internal data details of a class by making data as private. This is data abstraction. Similarly we hide the functionality of a function to outside world, which is function abstraction.
  3. both are true
  4. none
Show Answer
Share Facebook gmail Twitter
11
What is abstract factory design pattern in Java?
  1. Abstract Factory design pattern are
    creational design pattern and use to decouple clients 
    from creating object they need,
    Abstract Factory design pattern adds another layer of abstraction over Factory
    Pattern and Abstract Factory implementation itself e.g. Abstract Factory will allow you to
    choose a particular Factory implementation based upon need which will then produce different kinds of products.
Show Answer
Share Facebook gmail Twitter
12
What is abstract interface?
  1. Every interface is implicitly abstract. This modifier is obsolete and should not be used in new programs.
  2. An interface that is declared as abstract, is called as abstract interface. If an interface contains all abstract methods, then it has to be declared like that.
  3. An interface that contains only abstract methods is called as abstract interface.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
13
Give one abstraction example?
  1. Making the variables of a class as private is one example of data abstraction.
  2. Assume that class A of package 1 contains a protected method fun(). Same class is extended by class B of package 2. Assume that package 2 contains one more class C. Now protected method fun() of class A of package 1 is abstracted for class C of package 2. Only class B of package 2 can see and use that method. Class C can't see and use that method. This is one kind of method or function abstraction.
  3. Accessing a public method of a class from other class with out knowing the internal implementation of that method, is an example of abstraction. 
  4. all are true.
Show Answer
Share Facebook gmail Twitter
14
What are the interface naming conventions in Java?
  1. The naming convention of an interface is similar to naming convention of a class in java. Names should be in CamelCase. They tend to have a name that describes an operation that a class can do. 
    Eg:
    interface Comparable{
    ...
    }
    interface Enumerable{
    ...
    }
  2. Some programmers like to distinguish interfaces by beginning the name with an "I":
    interface IComparable{
    ...
    }
    interface IEnumerable{
    ...
    }
  3. both are true
  4. none
Show Answer
Share Facebook gmail Twitter
15
What is interface variable? Is it possible to have variables in an interface just like a class?
  1. Any variable declared in an interface should be public static final.
  2. Programmer has to give initialization of a variable declared in an interface in declaration only. Because only constants are allowed in interface. And constants has to be initialized in the declaration itself.
  3. It is not possible to have normal variables in an interface. Only constants are allowed.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
16
What is an interface ?
  1. An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. The class implementing an interface has to give the implementations for all the methods of that interface, or else it should be declared itself as an abstract class.
    Interface can contain only below things.
        1.Constants.
        2.Function declarations.
        3.Nested types(classes or Interfaces).
    
  2. Interfaces are generally used as APIs for accessing 3rd party libraries or server side libraries. Interface is used as a security mechanism to hide the library code, when delivering or selling libraries to clients. Clients uses interfaces to understand the features and functions available in the library, and hence access those function by using interface variable.
  3. Interfaces can also be used as a mechanism to achieve multiple inheritance of classes, indirectly.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
17
What is interface implementation?
  1. When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.
  2. interfaces can only be extended, but can't be implemented.
  3. option 1 is true, but if the class that implements the interface doesn't give the implementation for all the interface methods, then it is not necessery to make that class as abstract class.
  4. none
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Read more papers :