Interface:
An interface contains only abstract methods. All methods are incomplete.
ex:
Interface i
{
public abstract void test();
}
In above example we have one method "test" but
not having body or any implementation to that method.like this we can have many abstract methods in interface.
Example program:
Step1: Created an interface vehicle and 3 unimplemented methods.
package interfaceexp;
public interface vehicle
{
public void accelerate();
public void steering();
public void brake();
}
Step2: Created a class Audi and implemented vehicle interface
package interfaceexp;
public class Audi implements vehicle
{
@Override
public void accelerate()
{
System.out.println("Audi car accelerate");
}
@Override
public void steering()
{
System.out.println("Audi car steering");
}
@Override
public void brake()
{
System.out.println("Audi car brake");
}
}
Step3: Created a another class Benz and implemented vehicle interface.
package interfaceexp;
public class Benz implements vehicle
{
@Override
public void accelerate()
{
System.out.println("Benz car accelerate");
}
@Override
public void steering()
{
System.out.println("Benz car steering");
}
@Override
public void brake() \
{
System.out.println("Benz car brake");
}
}
Step 4:
package interfaceexp;
public class Driver
{
public static void main(String[] args)
{
vehicle v=new Audi();
v.accelerate();
v.brake();
v.steering();
System.out.println("---------------------");
vehicle v1=new Benz();
v1.accelerate();
v1.brake();
v1.accelerate();
}
}
Ouput:
Audi car accelerate
Audi car brake
Audi car steering
---------------------
Benz car accelerate
Benz car brake
Benz car accelerate
An interface contains only abstract methods. All methods are incomplete.
ex:
Interface i
{
public abstract void test();
}
In above example we have one method "test" but
not having body or any implementation to that method.like this we can have many abstract methods in interface.
- We are using the interface to get 100% "incompleteness".
- Inside interface we cannot keep complete or concrete method.
- Inside interface we should have only methods declaration part.We cannot keep defintion part.
- In interface each and every "Method" by default are"public abstract".
- In interface each and every "Variable" by default are"public static final".
- In interface there is no constructor business.So it is 100% absract.
Variable inside Interface:
- If interface is having default variable then need to assign value to variable because by default variable are "public static final".
- So final variable should give value to the variable.
Example program:
Step1: Created an interface vehicle and 3 unimplemented methods.
package interfaceexp;
public interface vehicle
{
public void accelerate();
public void steering();
public void brake();
}
package interfaceexp;
public class Audi implements vehicle
{
@Override
public void accelerate()
{
System.out.println("Audi car accelerate");
}
@Override
public void steering()
{
System.out.println("Audi car steering");
}
@Override
public void brake()
{
System.out.println("Audi car brake");
}
}
package interfaceexp;
public class Benz implements vehicle
{
@Override
public void accelerate()
{
System.out.println("Benz car accelerate");
}
@Override
public void steering()
{
System.out.println("Benz car steering");
}
@Override
public void brake() \
{
System.out.println("Benz car brake");
}
}
package interfaceexp;
public class Driver
{
public static void main(String[] args)
{
vehicle v=new Audi();
v.accelerate();
v.brake();
v.steering();
System.out.println("---------------------");
vehicle v1=new Benz();
v1.accelerate();
v1.brake();
v1.accelerate();
}
}
Ouput:
Audi car accelerate
Audi car brake
Audi car steering
---------------------
Benz car accelerate
Benz car brake
Benz car accelerate
No comments:
Post a Comment