Click here to Skip to main content
15,609,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to the OOP concept and I wish to receive some clarification on the matter. The interaction of an object can be set in the class and so my question is what is the purpose of creating an interface when methods are already established in the blueprint?

Implementation:
package objectOrientedProgrammingConcepts;

public class Door {
	
	String state = "shut";
	
	void changeState(String newState) {
		state = newState;
	}
	
	void printState() {
		System.out.println("The door is " + state + ".");
	}
}


Object Creation:
package objectOrientedProgrammingConcepts;

public class DoorDemo {

	public static void main(String[] args) {
		
		Door door1 = new Door();
		Door door2 = new Door();
		
		door1.changeState("open");
		door2.changeState("shut");
		
		door1.printState();
		door2.printState();
	}
}


What I have tried:

- Java's Documentation
- Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language)[^]
Posted
Updated 16-Apr-17 13:31pm
v2
Comments
[no name] 16-Apr-17 19:09pm    
Maybe you should read the articles that you link to, https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

1 solution

What you need to learn here are:

1. Inheritance
2. Polymorphism

And then later on, in the advanced concepts, you also need to understand how an API is developed. Probably, in most cases, an interface is used where you need to define a structure of your contract — in APIs, especially where you are using interfaces etc, you define contracts, instead of simple objects. The reason is, an interface must be implemented. That interface, can be used to ensure that the client applications, using our API, has written an implementation of our standard — structure etc.

Interfaces are not only used in Java, they are also used in C# and C++ (although, C++ does not provide any keyword interface). The logical usage of them is applied when you are developing a client application, I would not use (nor recommend) considering them in a plain application.

Need an example? Sure, read the tutorial in the Oracle documents, they provide a very good example of a Bicycle API,
Java
interface Bicycle {

    //  wheel revolutions per minute
    void changeCadence(int newValue);

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}

Now, if you want to create an implementation of this standard, then you implement all the interface itself,
Java
class ACMEBicycle implements Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

   // The compiler will now require that methods
   // changeCadence, changeGear, speedUp, and applyBrakes
   // all be implemented. Compilation will fail if those
   // methods are missing from this class.

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;   
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

Notice the empty method bodies in the interface, and the addition of bodies to the same functions in the class. There is one more extra thing in the class, can you guess that?

I would leave the rest up to you, please also open up NetBeans and try to mess around here and there with a few things to understand how they work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900