java interface vs abstract class example code download tutorial example What are the difference between interface and abstract class in Java all explain in the following Example code in java
public interface Moveable {
int AVG_SPEED=40;
void move();
void speedUp(int increment);
void applyBrakes(int decrement);
}
abstract class Vehicle {
String Name;
int RegNo;
Vehicle(String nm,int rn){
this.Name=nm;
this.RegNo=rn;
}
public void DisplayInfo() {
System.out.println("Vehicle Name : "+this.Name);
System.out.println("Vehicle Reg No : "+this.RegNo);
}
public abstract void start();
}
public class Car extends Vehicle implements Moveable{
float speed;
Car(String nm,int rn){
super(nm,rn);
}
@Override
public void speedUp(int increment) {
this.speed+=increment;
System.out.println("Speed Up Car");
System.out.println("Current Speed:"+speed);
}
@Override
public void applyBrakes(int decrement) {
this.speed-=decrement;
System.out.println("Apply Brake in Car");
System.out.println("Current Speed:"+speed);
}
@Override
public void move() {
this.speed=AVG_SPEED;
System.out.println("Current Speed:"+speed);
}
@Override
public void start() {
this.speed=0;
}
}
public class Interfaceexample {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Car cObj=new Car("BMW",1002);
cObj.DisplayInfo();
cObj.start();
cObj.move();
cObj.speedUp(10);
cObj.speedUp(20);
cObj.applyBrakes(5);
}
}
0 comments:
Post a Comment