Java Interfaces & Abstraction Practice
Interfaces, abstract classes, and implementing contracts.
1
Basic Interface
Easy
Define an interface Drawable with a method void draw(). Create a class Circle that implements Drawable and prints "Drawing a circle". In main, create a Circle and call draw().
Input Format
No input.
Output Format
The draw() output.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Drawing a circle |
Constraints
—
Explanation
Implement the interface method.
2
Abstract Class
Easy
Create an abstract class Animal with an abstract method void sound(). A subclass Cow extends Animal and implements sound() to print "Moo". In main, create a Cow and call sound().
Input Format
No input.
Output Format
The sound output.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Moo |
Constraints
—
Explanation
Extend the abstract class and implement the abstract method.
1
Multiple Interfaces
Medium
Define interfaces Runnable with void run() and Swimmable with void swim(). A class Amphibian implements both, printing "Running" and "Swimming". In main call both methods.
Input Format
No input.
Output Format
Two lines: Running, then Swimming.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Running Swimming |
Constraints
—
Explanation
A class may implement several interfaces.
2
Interface Field (Constant)
Medium
Define an interface Constants with int MAX = 100. In main print Constants.MAX.
Input Format
No input.
Output Format
100
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
100 |
Constraints
—
Explanation
Interface fields are public static final.
3
Default Method
Medium
Create an interface Greeter with a default method void greet() that prints "Hello" and an abstract method void greetByName(String name). Implement it in a class so greet() is inherited and greetByName prints "Hello, !". Read a name and call both methods.
Input Format
A single line containing a name.
Output Format
Two lines: Hello, then Hello, !.
Sample Test Cases
|
Sample Input 1
Alice |
Sample Output 1
Hello Hello, Alice! |
Constraints
1
Explanation
Implement only the abstract method; default is inherited.
4
Abstract and Concrete Methods
Medium
Create an abstract class Shape with a concrete method void info() printing "Shape info" and an abstract method double area(). A subclass Square stores side and implements area() as side*side. Read a side, create a Square, print info() then the integer part of area().
Input Format
A single integer S.
Output Format
Two lines: Shape info, then floor(area).
Sample Test Cases
|
Sample Input 1
6 |
Sample Output 1
Shape info 36 |
Constraints
1
Explanation
Concrete methods are inherited as-is.
1
Interface Reference
Hard
Define an interface Vehicle with void start(). Classes Car and Bike implement it printing "Car started" / "Bike started". Read a word ("car" or "bike"), create the object as a Vehicle reference, and call start().
Input Format
A single word.
Output Format
The correct start() output.
Sample Test Cases
|
Sample Input 1
car |
Sample Output 1
Car started |
|
Sample Input 2
bike |
Sample Output 2
Bike started |
Constraints
—
Explanation
Polymorphism through the interface type.
2
Anonymous Class
Hard
Create an interface Clickable with void onClick(). In main, create a Clickable using an anonymous class whose onClick() prints "Clicked!". Call onClick().
Input Format
No input.
Output Format
Clicked!
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Clicked! |
Constraints
—
Explanation
Anonymous classes implement the interface inline.
3
Lambda for Functional Interface
Hard
Define an interface MathOperation with int operate(int a, int b). In main assign it a lambda that returns a + b. Read two integers and print operation.operate(a, b).
Input Format
A single line with two integers.
Output Format
A single integer: the sum.
Sample Test Cases
|
Sample Input 1
7 8 |
Sample Output 1
15 |
|
Sample Input 2
-3 10 |
Sample Output 2
7 |
Constraints
-10^6
Explanation
MathOperation op = (a, b) -> a + b;
4
Interface Inheritance
Hard
Define interface A with void methodA() and interface B extends A with void methodB(). A class C implements B implementing both. In main call both methods printing "Method A" and "Method B".
Input Format
No input.
Output Format
Two lines: Method A, then Method B.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Method A Method B |
Constraints
—
Explanation
An interface can extend another interface.
Competitive MCQs — Java Interfaces & Abstraction
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 32
Q1
Which keyword declares an interface?
Correct!
Wrong — correct answer is .
interface defines a contract of abstract methods.
Q2
Which keyword makes a class implement an interface?
Correct!
Wrong — correct answer is .
A class implements an interface with the implements keyword.
Q3
What is an abstract class?
Correct!
Wrong — correct answer is .
Abstract classes are base templates; new on them fails.
Q4
Which annotation marks an abstract method in Java?
Correct!
Wrong — correct answer is .
Methods are declared abstract with the modifier, not an annotation.
Q5
What is the output of this code?
java
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
System.out.println("Circle");
}
}
new Circle().draw();Correct!
Wrong — correct answer is .
Circle implements draw(), so calling it prints "Circle".
Q6
What is the output of this code?
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
System.out.println("Circle");
}
}
new Circle().draw();
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
System.out.println("Circle");
}
}
new Circle().draw();
Correct!
Wrong — correct answer is .
Circle implements draw(), so calling it prints "Circle".
Q7
Can an interface have a default (concrete) method?
Correct!
Wrong — correct answer is .
Since Java 8, interfaces may provide default methods.
Q8
What is the output of this code?
// Shape s = new Shape(); -> ?
// Shape s = new Shape(); -> ?
java
1
2
3
2
3
abstract class Shape {
abstract double area();
}Correct!
Wrong — correct answer is .
Abstract classes cannot be instantiated.
Q9
What is the output of this code?
abstract class Shape {
abstract double area();
}
// Shape s = new Shape(); -> ?
abstract class Shape {
abstract double area();
}
// Shape s = new Shape(); -> ?
Correct!
Wrong — correct answer is .
Abstract classes cannot be instantiated.
Q10
What is the main purpose of interfaces?
Correct!
Wrong — correct answer is .
Interfaces specify what implementing classes must provide.
Q11
How many interfaces can a single class implement?
Correct!
Wrong — correct answer is .
A class may implement multiple interfaces (comma-separated).
Q12
Which of these is a functional interface?
Correct!
Wrong — correct answer is .
Functional interfaces (e.g. Runnable) have one abstract method.
Q13
What is the output of this code?
interface A {
void run();
}
class B implements A {
public void run() {
System.out.println("run");
}
}
new B().run();
interface A {
void run();
}
class B implements A {
public void run() {
System.out.println("run");
}
}
new B().run();
Correct!
Wrong — correct answer is .
B provides run(), which prints "run".
Q14
What access modifier do interface methods have by default?
Correct!
Wrong — correct answer is .
Interface methods are implicitly public and abstract.
Q15
Can an interface extend another interface?
Correct!
Wrong — correct answer is .
Interfaces extend other interfaces to combine contracts.
Q16
What is the output of this code?
interface A {
default void hi() {
System.out.println("Hi");
}
}
class B implements A { }
new B().hi();
interface A {
default void hi() {
System.out.println("Hi");
}
}
class B implements A { }
new B().hi();
Correct!
Wrong — correct answer is .
Default methods give implementing classes a concrete behavior.
Q17
What is an abstract method?
Correct!
Wrong — correct answer is .
Abstract methods end with a semicolon and have no implementation.
Q18
Can you instantiate an interface directly with new?
Correct!
Wrong — correct answer is .
Interfaces have no constructors; classes implement them.
Q19
What is the output of this code?
abstract class A {
abstract void f();
}
class B extends A {
void f() {
System.out.println("f");
}
}
new B().f();
abstract class A {
abstract void f();
}
class B extends A {
void f() {
System.out.println("f");
}
}
new B().f();
Correct!
Wrong — correct answer is .
B implements the abstract f(), printing "f".
Q20
What is the purpose of an interface?
Correct!
Wrong — correct answer is .
Interfaces specify required behavior without implementation details.
Q21
How many interfaces can a class implement?
Correct!
Wrong — correct answer is .
Implementations are comma-separated after implements.
Q22
Which statement about abstract classes is TRUE?
Correct!
Wrong — correct answer is .
Abstract classes mix implemented methods with abstract requirements.
Q23
What is the output of this code?
interface A {
int X = 5;
}
System.out.println(A.X);
interface A {
int X = 5;
}
System.out.println(A.X);
Correct!
Wrong — correct answer is .
Interface fields are public static final constants.
Q24
Which keyword makes a class abstract?
Correct!
Wrong — correct answer is .
Declaring a class abstract means it may contain abstract methods.
Q25
Can an abstract class have constructors?
Correct!
Wrong — correct answer is .
Abstract classes have constructors that subclasses invoke via super().
Q26
What is the output of this code?
interface A {
void f();
}
abstract class B implements A {
public abstract void g();
}
class C extends B {
public void f() {}
public void g() {}
}
System.out.println("ok");
interface A {
void f();
}
abstract class B implements A {
public abstract void g();
}
class C extends B {
public void f() {}
public void g() {}
}
System.out.println("ok");
Correct!
Wrong — correct answer is .
C implements both abstract methods, so it compiles and prints "ok".
Q27
What is a default method in an interface?
Correct!
Wrong — correct answer is .
Default methods add behavior while keeping backward compatibility.
Q28
Which is TRUE about interface variables?
Correct!
Wrong — correct answer is .
Every interface field is a compile-time constant.
Q29
What is the output of this code?
interface A {
static void show() {
System.out.println("static");
}
}
A.show();
interface A {
static void show() {
System.out.println("static");
}
}
A.show();
Correct!
Wrong — correct answer is .
Static interface methods are called through the interface name.
Q30
What is the relationship between a class and the interface it implements?
Correct!
Wrong — correct answer is .
A concrete class must implement all inherited abstract methods.
Q31
When is a class forced to stay abstract?
Correct!
Wrong — correct answer is .
Unimplemented abstract methods keep the class abstract.
Q32
Which of these can implement an interface?
Correct!
Wrong — correct answer is .
Classes (including abstract ones and records) can implement interfaces.