OOPM JAVA - Create an Interface ‘Vehicle’ which has abstract methods

 Experiment No. 11

Aim: Create an Interface ‘Vehicle’ which has abstract methods changeGear(), speedUp(),applyBrakes().Create a classes like ‘Bike’ ,’Car’ which will implement functionality of ‘Vehicle’ in their own way 

Objective: To make aware of Interface concept. 

Outcome: Students implemented Interface and Inheritance.

Theory:

Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default.

Methods in interfaces do not have body, they have to be implemented by the class before you can access them. The class that implements interface must implement all the methods of that interface. Also, java programming language does not allow you to extend more than one class, However you can implement more than one interfaces in your class.

 Syntax:

Interfaces are declared by specifying a keyword “interface”.


Int interface MyInterface

{

   /* All the methods are public abstract by default

    * As you see they have no body

    */

   public void method1();

   public void method2();

}

Interface and Inheritance

An interface can not implement another interface. It has to extend the other interface. See the below example where we have two interfaces Inf1 and Inf2. Inf2 extends Inf1 so If class implements the Inf2 it has to provide implementation of all the methods of interfaces Inf2 as well as Inf1.

interface Inf1{

   public void method1();

}

interface Inf2 extends Inf1 {

   public void method2();

}

public class Demo implements Inf2{

   /* Even though this class is only implementing the interface Inf2, it has to implement all the

    * methods of Inf1 as well because the interface Inf2 extends Inf1 */

    public void method1(){

    System.out.println("method1");

    }

    public void method2(){

    System.out.println("method2");

    }

    public static void main(String args[]){

    Inf2 obj = new Demo();

    obj.method2();

    }

}

Algorithm:

  1. Start
  2. Create an Interface ‘Vehicle’.
  3. Vehicle has abstract methods changeGear(), speedUp(), and applyBrakes().
  4. Create a classes like ‘Bike’ ,’Car’.
  5. Both Bike and Car Class will implement functionality of ‘Vehicle’ in their own way 
  6. Stop.


Code : 

import java.io.*; 

interface Vehicle { 

    void changeGear(int a); 

    void speedUp(int a); 

    void applyBrakes(int a); 

class Bicycle implements Vehicle{ 

    int speed; 

    int gear; 

    @Override

    public void changeGear(int newGear){ 

        gear = newGear; 

    } 

    @Override

    public void speedUp(int increment){ 

        speed = speed + increment; 

    }

    @Override

    public void applyBrakes(int decrement){ 

        speed = speed - decrement;   

    }

    public void printStates() { 

         System.out.println("speed: " + speed 

              + " gear: " + gear); 

    } 

class Bike implements Vehicle { 

    int speed; 

    int gear; 

 

    @Override

    public void changeGear(int newGear){

        gear = newGear; 

 

    }

    @Override

    public void speedUp(int increment){ 

      speed = speed + increment; 

    } 

    @Override

    public void applyBrakes(int decrement){ 

        speed = speed - decrement; 

    } 

    public void printStates() { 

 

         System.out.println("speed: " + speed 

            + " gear: " + gear); 

    }       

class PresentState { 

    public static void main (String[] args) { 

        Bicycle bicycle = new Bicycle(); 

        bicycle.changeGear(2); 

        bicycle.speedUp(3); 

        bicycle.applyBrakes(1); 

        System.out.println("Bicycle present state :"); 

        bicycle.printStates(); 

        Bike bike = new Bike(); 

        bike.changeGear(1); 

        bike.speedUp(4); 

        bike.applyBrakes(3); 

        System.out.println("Bike present state :"); 

        bike.printStates(); 

    } 


Output: 

Create an Interface ‘Vehicle’ which has abstract methods


Observations and learning: 

In this experiment we get to know about the concept of interface and how the interface differs from the concept of inheritance.


Conclusion: 

Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract.

 

Question of Curiosity 

1. Can a class implement more than one interface in Java?

Ans : A class can implement more than one interface at a time. 

 

2. Can an interface extends more than one interface in Java?

Ans : A class can extend only one class, but implement many interfaces. An interface can extend another interface, in a similar way as a class can extend another class.

 

3. What will happen if a class extends two interfaces and they both have a method with same name and signature?

Ans : If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously.

 

4. Can we create object for an interface?

Ans : We can’t create interface but we can make reference of it that refers to the Object of its implementing class.A class can implement more than one interface.


Comments

  1. This post is really nice and informative. Mobile Point of Sale (POS) systems allow businesses to process transactions using mobile devices like smartphones or tablets. These systems offer flexibility and convenience, enabling salespeople to accept payments anywhere. Mobile POS solutions often integrate with inventory management and customer relationship management (CRM) systems, providing a comprehensive solution for businesses on the go. For comprehensive insights into our mobile pos, please click here to visit our detailed page and explore how it can meet your needs.

    ReplyDelete

Post a Comment