Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet40/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   36   37   38   39   40   41   42   43   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

101.
a.
b.
c.
d.
e.
102.
CHAPTER
09
Abstract Classes and Interfaces
What are the possible ways of declaring an interface?
Answer:
Following are the possible ways of declaring an interface:
interface Interface1 {}
public interface Interface2 {}
public abstract interface Interface3 {}
abstract interface Interface4 {}
abstract public interface Interface5 {}
So, an interface can either have the public access specifier or no
specifier which means it will have default access. It can also have
the abstract keyword. The order of the keywords does not matter,
they can be specified in any order.
Explain abstract class with a code sample.
Answer:
An abstract class is a class with zero or more abstract methods.
An abstract method is a method that has no body. Both the
abstract method as well as the class needs to have the abstract
keyword specified. The following code demonstrates an abstract
class:
public abstract class Animal {
private String name;


103.
public abstract void speak();
public void printName(){
System.out.println(name);
}
}
Here, Animal is an abstract class. It has a method called speak
which is abstract. It also has a concrete method called
printName.
Explain with a code sample how to implement an
interface.
Answer:
In order to implement an interface, you need to use the
implements keyword after the class name. The implements
keyword needs to be followed by the interface name. You also
need to provide an implementation for the methods in the
interface. The following code demonstrates this:
public interface ShapeDrawer {
public void draw();
}
This code defines an interface called ShapeDrawer with a
method called draw. The following is a class that implements this
interface:
public class Triangle implements ShapeDrawer{
public void draw() {
// code to draw shape
}
}



Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   36   37   38   39   40   41   42   43   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling