Menu
Topics Index
...
`

Interface2962


If you need explanation Read this topic

If you need Answer Take test on this topic

Interface2962
What will be the output of the following program?
public class King {
    public static void main(String[] args) {
        King k = new King();
        System.out.print("Output = " + k.new Elephant().step2(2, 3));
    }
    interface Queen {
        float step2(int low, int high);
    }
    interface Pawn {
        float step3(int a, int b, int c);
    }
    abstract class Knight implements Queen, Pawn {
    }
    class Elephant implements Queen {
        public float step2(int x, int y) {
            return (float)(x * 3);
        }
    }
}

A. Output = 6f
B. Output = 6.0
C. Output = 6.0f
D. Some other output
E. Compilation Error or Runtime Error
Topic: Difference Between Interfaces And Abstract Classes

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is B.
There is no compilation error.. we are creating King object and creating Elephant  object on it and calling step2() by passing 2 and 3.. so we will get 6.0 as floating point...

Posted by Gopinath Manchikanti    2014-12-03 03:44:11


ans b.

An interface in java is a blueprint of a class. It has static constants and abstract methods only.

The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java.

There are mainly three reasons to use interface. They are given below.

It is used to achieve fully abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling.

In other words, Interface fields are public, static and final bydefault, and methods are public and abstract.

If a class implements multiple interfaces, or an interface extends multiple interfaces

Interface can be used to define a generic template and then one or more abstract classes to define partial implementations of the interface. Interfaces just specify the method declaration (implicitly public and abstract) and can only contain fields (which are implicitly public static final). Interface definition begins with a keyword interface. An interface like that of an abstract class cannot be instantiated.

coming to this program ,here we have 2 class 1 abstract class and 2 interfaces,
in main class step2() called with 2 parameter,elephant class also implements queen interface ,this interface also has the same funtion.x=2,y=3 step2()return float value of x*3=2*3=6=6.0
here we use explicit conversion .

Posted by Maheshwari Natarajan    2014-12-03 08:01:07


b

Posted by Abhishek Kumar    2014-12-04 02:08:01


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Sai Ram,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-05 04:35:29


© meritcampus 2019

All Rights Reserved.

Open In App