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
}
}