Interface is even more abstract then an abstract class

AI Thread Summary
An interface in Java, such as "Furniture," serves as a contract that defines methods a class must implement without containing variables. It allows for loose coupling between classes, enabling an object to reference the interface rather than specific classes. This means that classes like "Chair" and "Table" can implement the "Furniture" interface by defining the required methods. While interfaces cannot have instance variables, they can include static variables. For example, constants like CHAIR and TABLE can be defined with specific values, and methods like "int getType();" can be implemented to return these values. Understanding interfaces is crucial for designing flexible and maintainable code in Java.
courtrigrad
Messages
1,236
Reaction score
2
Hello all

I know an interface is even more abstract then an abstract class. Let's say I want to make an interface called Furniture that implements two class called Chair and Table. What does this exactly mean? Can someone please clarify this? Thanks

Code:
 public interface Furniture {

int quantity;
boolean table;

Code:
 public class Chair implements Furniture {

public GetQuant()

Now what do i put in the two classes?

Thanks
 
Computer science news on Phys.org
I'm not too sure myself, cos it's been ages since I've done Java (!). You might like to read the http://java.sun.com/docs/books/tutorial/java/interpack/interfaceDef.html in the Java tutorial from Sun.
 
Last edited by a moderator:
Interfaces can't have variables. An interface defines what function a class must implement if it declares itself to implement the interface. This allows you to declare an object that has the type interface, and then call the functions that the interface declared to several different classes of objects.
 
A crucial role of interfaces is to ensure loose coupling between classes. If a class shouldn't have knowledge of other classes, it can have a reference of the interface that those classes implement. For example if you write a GUI program MyProgram that handles events from a JButton, the JButton implementation doesn't need to have a reference to class MyProgram, but to interface ActionListener, which class MyProgram implements.
New syntax with interfaces includes kewords interface and implements.
 
Interfaces, while they may not have "variables" may include static variables, though. For example, you could have CHAIR, TABLE, and ETC with values of 1, 2, and 3, and have a function definition "int getType();" which would return one of these numbers.

It was fun in my java class. I had some interfaces that I had to implement, and I even had a "class Name extends Name1 implements Name2". That was fun...
 
This week, I saw a documentary done by the French called Les sacrifiés de l'IA, which was presented by a Canadian show Enquête. If you understand French I recommend it. Very eye-opening. I found a similar documentary in English called The Human Cost of AI: Data workers in the Global South. There is also an interview with Milagros Miceli (appearing in both documentaries) on Youtube: I also found a powerpoint presentation by the economist Uma Rani (appearing in the French documentary), AI...
Back
Top