1. An interface is a blueprint of a class. It has static constants and abstract methods.
Interface fields(data memebers) are public, static and final by default, and
methods are public and abstract.
or
2. It is a formal set of method and constant declarations that must be defined by the class that implements it.
- All methods in an interface are implicitly public, abstract, and never static.
- All variables in an interface are implicitly static, public, final. They cannot be transient or volatile. A class can shadow the variables it inherits from an interface, with its own variables
interface Moveable
{
int AVG-SPEED = 40;
void move();
}
class Vehicle implements Moveable
{
public void move()
{
System .out. print in ("Average speed is"+AVG-SPEED");
}
public static void main (String[] arg)
{
Vehicle vc = new Vehicle();
vc.move();
}
}
definition:-
- An interface is a specification of method prototypes.
- All interface methods should have public accessibility when implemented in class
- Interfaces cannot be declared final, since they are implicitly abstract.
- A class can implement two interfaces that have a method with the same signature or variables with the same name
Summary on interfaces:-
1.An interface is a specification of method prototypes.
2.An interface contains only abstract methods.
3.An interface contains 0 (or) more abstract methods.
4.All the methods of the interface are public and abstract by default.
i.e An interface is implicitly abstract
Methods in an interface are implicitly public.
5.An interface can contain variables which are public, static and final by default.
6.We cannot create an object to interface.
7.We can create a reference of interface type.
8.All the methods of the interface should be implemented in its implementation class.
9.Interface reference can be used to refer to objects of its implementaion classes.
10.If any method is not implemented,then that implementation class should be declared as 'abstract'.
11.An interface can not implement another interface.
12.An interface can extend another interface. (i.e interface inheritance)
13.A class can implement multiple interfaces.
14. Methods inside Interface must not be static, final, native or strictfp.
15. All variables declared inside interface are implicitly public static final variables
16. All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword
17. An interface does not contain any constructors.
Interface can extend one or more other interface.
interface NewsPaper
{
news();
}
interface Magazine extends NewsPaper
{
colorful();
}
Interface cannot implement a class.
Interface can be nested inside another interface.
what is the difference between abstract class and interface :
1. A programmer creates an abstract class when there are some common features shared by all the objects.A programmer writes an interface when every feature should be implemented differently for different objects.
2.When an abstract class is written,it is the duty of the programmer to provide sub classes to it. When an interface is written,the programmer can leave implementation for the third party vendors.
Multiple inheritance using interfaces indirectly :
Java does not support multiple inheritance. But we can achieve the multiple inheritance in java using multiple interfaces.
Interfaces supports Multiple Inheritance
interface Moveable
{
boolean isMoveable();
}
interface Rollable
{
boolean isRollable
}
class Tyre implements Moveable, Rollable
{
int width;
boolean isMoveable()
{
return true;
}
boolean isRollable()
{
return true;
}
public static void main(String args[])
{
Tyre tr=new Tyre();
System.out.println(tr.isMoveable());
System.out.println(tr.isRollable());
}
}
Output:
true
true
Note: A class implements interface but
One interface extends another interface .
class can implement any number of interfaces
- interface Printable
- {
- void print();
- }
- interface Showable
- {
- void show();
- }
- class A implements Printable,Showable
- {
- public void print()
- {
- System.out.println("Hello");
- }
- public void show()
- {
- System.out.println("Welcome");
- }
- public static void main(String args[]){
- A a = new A();
- a.print();
- a.show();
- }
- }
Output:Hello
Welcome
What is marker or tagged interface ?
An interface that have no member is known as marker or tagged interface. For example: Serializable, Cloneable, Remote etc. They are used to provide some essential information to the JVM so that JVM may perform some useful operation.
|
- //How Serializable interface is written?
- public interface Serializable
- {
- }
What is a cloneable interface and how many methods does it contain?
Ans- It is not having any method because it is a TAGGED or MARKER interface
What is the difference between abstract class and interface?
Ans: a) All the methods declared inside an interface are abstract whereas
abstract class must have at least one abstract method and others may be concrete or abstract.
b) In abstract class, key word abstract must be used for the methods
whereas interface we need not use that keyword for the methods.
c) Abstract class must have subclasses whereas interface can't have subclasses
What must a class do to implement an interface?
It must provide all of the methods in the interface and identify the interface in its implements clause.
What's the difference between an interface and an abstract class? Also discuss the similarities. (Very Important)
Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. Interface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method definition for all the methods
Abstract class is a Class prefix with a abstract keyword followed by Class definition. Interface is a Interface which starts with interface keyword.
Abstract class contains one or more abstract methods. where as Interface contains all abstract methods and final declarations
Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes. Interfaces are useful in a situation that all properties should be implemented.
Differences are as follows:
* Interfaces provide a form of multiple inheritance. A class can extend only one other class.
* Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
* A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
interface extends other interface, An interface can not implement another interface.An interface can extend multiple interfaces
* Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.
Similarities:
* Neither Abstract classes or Interface can be instantiated
- Abstract class is a class which contain one or more abstract methods, which has to be implemented by its sub classes
- Abstract class is a Class prefix with an abstract keyword followed by Class definition.
- Abstract class can also contain concrete methods.Whereas, Interface contains all abstract methods and final variable declarations. Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes
- Interface is a Java Object containing method declaration but no implementation. The classes which implement the Interfaces must provide the method definition for all the methods
- Interface is a pure abstract class which starts with interface keyword.
- Interfaces are useful in a situation that all properties should be implemented.
No comments:
Post a Comment