Interface is even more abstract then an abstract class

In summary, the conversation discusses the concept of interfaces in Java and their role in ensuring loose coupling between classes. An interface defines the functions that a class must implement if it declares itself to implement the interface, allowing for more flexibility in calling functions from different classes of objects. Interfaces do not have variables, but may include static variables and function definitions. The conversation also mentions some personal experiences and examples with implementing interfaces in Java.
  • #1
courtrigrad
1,236
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
  • #2
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:
  • #3
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.
 
  • #4
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.
 
  • #5
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...
 

1. What is an interface in programming?

An interface in programming is a template or blueprint for a class that defines a set of methods and properties that must be implemented by any class that uses it. It is a way to achieve abstraction and define common behaviors that can be shared by multiple classes.

2. How is an interface different from an abstract class?

An interface is different from an abstract class in that it cannot contain any implementation of methods or properties. It only defines the structure and name of these elements, leaving the implementation up to the class that implements the interface. Additionally, a class can implement multiple interfaces, but can only inherit from one abstract class.

3. Why is an interface considered even more abstract than an abstract class?

An interface is considered more abstract than an abstract class because it provides even less implementation details. It only defines the structure of methods and properties, without any actual code. This allows for greater flexibility and customization in the classes that implement the interface.

4. When should I use an interface instead of an abstract class?

You should use an interface instead of an abstract class when you want to define a common set of behaviors that can be shared by multiple classes, but the implementation of these behaviors may vary. Interfaces are also useful when a class needs to inherit from multiple sources, as it can only inherit from one abstract class but can implement multiple interfaces.

5. Can I create an instance of an interface?

No, you cannot create an instance of an interface. Interfaces only define the structure and name of methods and properties, but do not provide any implementation. They are meant to be implemented by classes, which can then be instantiated and used to create objects.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
682
  • Engineering and Comp Sci Homework Help
Replies
14
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Computing and Technology
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Sticky
  • Science and Math Textbooks
Replies
10
Views
5K
Replies
4
Views
1K
Back
Top