0

I've just started studying design patterns. While I was studying " Decorator Pattern " I noticed that we use Interface in order to instantiate an object.

I will demonstrate my example by using the decorator pattern.

public interface GuitarMets {

public String getDesc();
public double getCost();
}

This is my interface and this interface is implemented by another class that will be used for the creation.

And in my main class I have lines like this.

public class MainClass {

public static void main(String[] args){

    GuitarMets guitar = new Sound(new Guitar());
    System.out.println(guitar.getDesc());
    System.out.println(guitar.getCost());


    }
}

So what I could not figure out is that, since we cannot create an object from Interfaces, how we are able to use it in order to instantiate objects from the interface?

I hope my question is not weird and finds you well.

Mystheman
  • 19
  • 3
  • Variables != objects. You are not creating an interface, you are defining a variable which has the interface as a type, but you are creating an object of a concrete type (`Sound`, which apparently implements `GuitarMets`), and assign (the reference to) that object to the variable. – Mark Rotteveel Oct 25 '20 at 08:15
  • For the creation part, I do not have any problem at all. I know that we instantiate something and then refer it to a class. But what I could not understand is that whether we can instantiate an object by using Interfaces or not? – Mystheman Oct 25 '20 at 08:19
  • You can only instantiate concrete objects, not interfaces. Maybe you're thinking of anonymous classes. Otherwise it is unclear what you're really asking. – Mark Rotteveel Oct 25 '20 at 08:22
  • I may be unclear because I couldn't find another way of asking this question. But I think this is the example of program to an interface, not an implementation. I got confused because in my main method I use interface as the type of that object and as far as I know ( I may be mistaken ) when we just define class name + object name, we call it implementation. So when I saw the type as interface, I got confused. – Mystheman Oct 25 '20 at 08:26
  • `GuitarMets guitar` defines a **variable**, not an **object**. The object you create is of type `Sound`, which is a concrete class, not an interface (although it implements the interface). You assign the object of type `Sound` to a variable of type `GuitarMets`, as a consequence you can only access the object with the methods defined in `GuitarMets` (other methods defined in `Sound` still exist, but are not accessible through the variable `guitar` without casting). – Mark Rotteveel Oct 25 '20 at 08:29
  • Thank you so much! My last question is that for the variable creation, do we still call it as implementation or do we call it as an implementation when we use a concrete class? – Mystheman Oct 25 '20 at 08:35
  • Neither, implementation is what happens when you define the class itself (the class **implements** the interface). – Mark Rotteveel Oct 25 '20 at 08:36
  • Okay, I confused the terms. While I was adding the comments, It came to my mind as implementation instead of declaration. Thank you for your comments. – Mystheman Oct 25 '20 at 08:45

0 Answers0