Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Can you tell me one real time example for interface and abstract class?

user-image
Question added by PREMKUMAR GANGATHARAN , test engineer trainee , clorida infotech
Date Posted: 2013/12/21
Hatim Laxmidhar
by Hatim Laxmidhar , Self Employed , The Way It Works

Example:

A Vehicle can be an interface with a method. 

MotorVehicle and Aircraft  are abstract classes.

Car, Bus etc are subclass of MotorVehicle.

Airbus, Cargo Planes, fighter planes are subclass of aircraft.

 

As per http://docs.oracle.com

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.

More Questions Like This