Concrete
class implements all the operations of an object. It is created using the
keyword class. It contains only concrete methods including variables, blocks
and constructors. It is always created as sub class next to abstract class or
interface in object’s inheritance hierarchy. A method that has body is called
as concrete method. The modifiers allowed for a concrete class are – public,
final, abstract, strictfp.
Example –
abstract class
Bus{
public abstract void
engine();
public void
brakes()
{
System.out.println("Bus has two brakes");
}
}
class
RedBus extends Bus{
@Override
public void
engine() {
System.out.println("Red bus engine capacity is 40 kmph");
}
}
class
Volve extends Bus{
@Override
public void
engine() {
System.out.println("Volve engine capacity is 110 kmph");
}
}
class
Driver{
public void
assignBus(Bus b)
{
b.brakes();
b.engine();
}
}
class
Tesr1 {
public static void
main(String[] args) {
Driver d = new
Driver();
d.assignBus(new
Volve());
Driver d2 = new
Driver();
d2.assignBus(new
RedBus());
}
}
Output –
Bus has two brakes
Volve engine capacity is 110 kmph
Bus has two brakes
Red bus engine capacity is 40
kmph
No comments:
Post a Comment