I'm having difficulties understanding how the “interface” function is useful in Java?












0














I've looked up the "interface" function several places, but nowhere does it seem to actually explain the benefits of using it, or why I should use interfaces when writing my own future programs.



I finished the exercise below, where I used an interface named "Form" to describe the methods "circumference" and "area". Then I have 3 classes "Circle", "Rectangle" and "Square" where the variables from each form are input and calculated to finally retrieve the circumference and area of each form.



My problem is that after I finished the exercise, I'm struggling to really see the point of implementing this "Form" interface. I feel like I could have just ignored using an interface and then simply through inheritance, make each of the classes inherit the circumference and area methods and then just create objects for each of the forms at the end when compiling?



How did the interface make things easier for me?



public class FormCompiling {
public static void main(String args) {
Form f = {new Circle(1.5), new Rectangle(2.0,3.0), new Square(2.5)};
System.out.println("Area:");
for(int i = 0 ; i<f.length; i++) {
System.out.println(f[i].area());
}
}
}

public interface Form {
public double circumference();
public double area();
}

public class Circle implements Form {
double radius = 0;
double area = 0;
double circumference = 0;

Circle(double radius) {
this.radius = radius;
}

@Override
public double circumference() {
circumference = 2 * radius * Math.PI;
return circumference;
}

@Override
public double area() {
area = radius * radius * Math.PI;
return area;
}
}

public class Rectangle implements Form {
double length = 0;
double width = 0;
double area = 0;
double circumference = 0;

Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

@Override
public double circumference() {
circumference = (2 * length) + (2 * width);
return circumference;
}

@Override
public double area() {
area = length * width;
return area;
}
}

public class Square extends Rectangle implements Form {

Square(double length) {
super(length, length);
this.length = length;
}

@Override
public double circumference() {
circumference = 4 * length;
return circumference;
}

@Override
public double area() {
area = length * length;
return area;
}
}









share|improve this question


















  • 1




    Write a method that computes the sum of the areas of many forms. What type would you use for the argument of such a method? What would you use if Square, Rectangle and Circle did not implement the Form interface? If you introduce a new Triangle class, will you have to change anything to this method in the first case? And in the second case?
    – JB Nizet
    Nov 22 at 19:23












  • Possible duplicates: stackoverflow.com/questions/2586389/… stackoverflow.com/questions/4052621/… stackoverflow.com/questions/20463/… (PHP, but same applies to java)
    – omajid
    Nov 22 at 19:43
















0














I've looked up the "interface" function several places, but nowhere does it seem to actually explain the benefits of using it, or why I should use interfaces when writing my own future programs.



I finished the exercise below, where I used an interface named "Form" to describe the methods "circumference" and "area". Then I have 3 classes "Circle", "Rectangle" and "Square" where the variables from each form are input and calculated to finally retrieve the circumference and area of each form.



My problem is that after I finished the exercise, I'm struggling to really see the point of implementing this "Form" interface. I feel like I could have just ignored using an interface and then simply through inheritance, make each of the classes inherit the circumference and area methods and then just create objects for each of the forms at the end when compiling?



How did the interface make things easier for me?



public class FormCompiling {
public static void main(String args) {
Form f = {new Circle(1.5), new Rectangle(2.0,3.0), new Square(2.5)};
System.out.println("Area:");
for(int i = 0 ; i<f.length; i++) {
System.out.println(f[i].area());
}
}
}

public interface Form {
public double circumference();
public double area();
}

public class Circle implements Form {
double radius = 0;
double area = 0;
double circumference = 0;

Circle(double radius) {
this.radius = radius;
}

@Override
public double circumference() {
circumference = 2 * radius * Math.PI;
return circumference;
}

@Override
public double area() {
area = radius * radius * Math.PI;
return area;
}
}

public class Rectangle implements Form {
double length = 0;
double width = 0;
double area = 0;
double circumference = 0;

Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

@Override
public double circumference() {
circumference = (2 * length) + (2 * width);
return circumference;
}

@Override
public double area() {
area = length * width;
return area;
}
}

public class Square extends Rectangle implements Form {

Square(double length) {
super(length, length);
this.length = length;
}

@Override
public double circumference() {
circumference = 4 * length;
return circumference;
}

@Override
public double area() {
area = length * length;
return area;
}
}









share|improve this question


















  • 1




    Write a method that computes the sum of the areas of many forms. What type would you use for the argument of such a method? What would you use if Square, Rectangle and Circle did not implement the Form interface? If you introduce a new Triangle class, will you have to change anything to this method in the first case? And in the second case?
    – JB Nizet
    Nov 22 at 19:23












  • Possible duplicates: stackoverflow.com/questions/2586389/… stackoverflow.com/questions/4052621/… stackoverflow.com/questions/20463/… (PHP, but same applies to java)
    – omajid
    Nov 22 at 19:43














0












0








0







I've looked up the "interface" function several places, but nowhere does it seem to actually explain the benefits of using it, or why I should use interfaces when writing my own future programs.



I finished the exercise below, where I used an interface named "Form" to describe the methods "circumference" and "area". Then I have 3 classes "Circle", "Rectangle" and "Square" where the variables from each form are input and calculated to finally retrieve the circumference and area of each form.



My problem is that after I finished the exercise, I'm struggling to really see the point of implementing this "Form" interface. I feel like I could have just ignored using an interface and then simply through inheritance, make each of the classes inherit the circumference and area methods and then just create objects for each of the forms at the end when compiling?



How did the interface make things easier for me?



public class FormCompiling {
public static void main(String args) {
Form f = {new Circle(1.5), new Rectangle(2.0,3.0), new Square(2.5)};
System.out.println("Area:");
for(int i = 0 ; i<f.length; i++) {
System.out.println(f[i].area());
}
}
}

public interface Form {
public double circumference();
public double area();
}

public class Circle implements Form {
double radius = 0;
double area = 0;
double circumference = 0;

Circle(double radius) {
this.radius = radius;
}

@Override
public double circumference() {
circumference = 2 * radius * Math.PI;
return circumference;
}

@Override
public double area() {
area = radius * radius * Math.PI;
return area;
}
}

public class Rectangle implements Form {
double length = 0;
double width = 0;
double area = 0;
double circumference = 0;

Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

@Override
public double circumference() {
circumference = (2 * length) + (2 * width);
return circumference;
}

@Override
public double area() {
area = length * width;
return area;
}
}

public class Square extends Rectangle implements Form {

Square(double length) {
super(length, length);
this.length = length;
}

@Override
public double circumference() {
circumference = 4 * length;
return circumference;
}

@Override
public double area() {
area = length * length;
return area;
}
}









share|improve this question













I've looked up the "interface" function several places, but nowhere does it seem to actually explain the benefits of using it, or why I should use interfaces when writing my own future programs.



I finished the exercise below, where I used an interface named "Form" to describe the methods "circumference" and "area". Then I have 3 classes "Circle", "Rectangle" and "Square" where the variables from each form are input and calculated to finally retrieve the circumference and area of each form.



My problem is that after I finished the exercise, I'm struggling to really see the point of implementing this "Form" interface. I feel like I could have just ignored using an interface and then simply through inheritance, make each of the classes inherit the circumference and area methods and then just create objects for each of the forms at the end when compiling?



How did the interface make things easier for me?



public class FormCompiling {
public static void main(String args) {
Form f = {new Circle(1.5), new Rectangle(2.0,3.0), new Square(2.5)};
System.out.println("Area:");
for(int i = 0 ; i<f.length; i++) {
System.out.println(f[i].area());
}
}
}

public interface Form {
public double circumference();
public double area();
}

public class Circle implements Form {
double radius = 0;
double area = 0;
double circumference = 0;

Circle(double radius) {
this.radius = radius;
}

@Override
public double circumference() {
circumference = 2 * radius * Math.PI;
return circumference;
}

@Override
public double area() {
area = radius * radius * Math.PI;
return area;
}
}

public class Rectangle implements Form {
double length = 0;
double width = 0;
double area = 0;
double circumference = 0;

Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

@Override
public double circumference() {
circumference = (2 * length) + (2 * width);
return circumference;
}

@Override
public double area() {
area = length * width;
return area;
}
}

public class Square extends Rectangle implements Form {

Square(double length) {
super(length, length);
this.length = length;
}

@Override
public double circumference() {
circumference = 4 * length;
return circumference;
}

@Override
public double area() {
area = length * length;
return area;
}
}






java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 19:20









WoeIs

47210




47210








  • 1




    Write a method that computes the sum of the areas of many forms. What type would you use for the argument of such a method? What would you use if Square, Rectangle and Circle did not implement the Form interface? If you introduce a new Triangle class, will you have to change anything to this method in the first case? And in the second case?
    – JB Nizet
    Nov 22 at 19:23












  • Possible duplicates: stackoverflow.com/questions/2586389/… stackoverflow.com/questions/4052621/… stackoverflow.com/questions/20463/… (PHP, but same applies to java)
    – omajid
    Nov 22 at 19:43














  • 1




    Write a method that computes the sum of the areas of many forms. What type would you use for the argument of such a method? What would you use if Square, Rectangle and Circle did not implement the Form interface? If you introduce a new Triangle class, will you have to change anything to this method in the first case? And in the second case?
    – JB Nizet
    Nov 22 at 19:23












  • Possible duplicates: stackoverflow.com/questions/2586389/… stackoverflow.com/questions/4052621/… stackoverflow.com/questions/20463/… (PHP, but same applies to java)
    – omajid
    Nov 22 at 19:43








1




1




Write a method that computes the sum of the areas of many forms. What type would you use for the argument of such a method? What would you use if Square, Rectangle and Circle did not implement the Form interface? If you introduce a new Triangle class, will you have to change anything to this method in the first case? And in the second case?
– JB Nizet
Nov 22 at 19:23






Write a method that computes the sum of the areas of many forms. What type would you use for the argument of such a method? What would you use if Square, Rectangle and Circle did not implement the Form interface? If you introduce a new Triangle class, will you have to change anything to this method in the first case? And in the second case?
– JB Nizet
Nov 22 at 19:23














Possible duplicates: stackoverflow.com/questions/2586389/… stackoverflow.com/questions/4052621/… stackoverflow.com/questions/20463/… (PHP, but same applies to java)
– omajid
Nov 22 at 19:43




Possible duplicates: stackoverflow.com/questions/2586389/… stackoverflow.com/questions/4052621/… stackoverflow.com/questions/20463/… (PHP, but same applies to java)
– omajid
Nov 22 at 19:43












2 Answers
2






active

oldest

votes


















0














Yes, you could have a parent class with empty methods (or some of them implemented).. and you'd get a similar result. However, you would not get compile errors if you forgot to implement some of the methods.



An interface "forces" you to follow some contract; a parent class does not.
In fact, an interface is a contract.
Whoever decides to implement it, has to implement the contract.




  • interface = the "what" = contract = specification

  • class = the "how" = implementation






share|improve this answer





























    0














    The biggest use case for interfaces in Java is to facilitate Polymorphism. Polymorphism means having a single class behave like multiple other classes. Multiple inheritance is not supported in Java so you can't extend more than one class, but you can implement multiple interfaces. There are many useful docs about this topic, here is an official one from oracle to get you started: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html



    Interfaces are also useful as a "social contract" between a designer of a class/program and the developer who implements it. By implementing an interface you promise that your implementation follows specific behaviors. This allows for consistency amongst different implementations, which is especially useful when trying to collaborate with many developers on a single, large project.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437001%2fim-having-difficulties-understanding-how-the-interface-function-is-useful-in%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Yes, you could have a parent class with empty methods (or some of them implemented).. and you'd get a similar result. However, you would not get compile errors if you forgot to implement some of the methods.



      An interface "forces" you to follow some contract; a parent class does not.
      In fact, an interface is a contract.
      Whoever decides to implement it, has to implement the contract.




      • interface = the "what" = contract = specification

      • class = the "how" = implementation






      share|improve this answer


























        0














        Yes, you could have a parent class with empty methods (or some of them implemented).. and you'd get a similar result. However, you would not get compile errors if you forgot to implement some of the methods.



        An interface "forces" you to follow some contract; a parent class does not.
        In fact, an interface is a contract.
        Whoever decides to implement it, has to implement the contract.




        • interface = the "what" = contract = specification

        • class = the "how" = implementation






        share|improve this answer
























          0












          0








          0






          Yes, you could have a parent class with empty methods (or some of them implemented).. and you'd get a similar result. However, you would not get compile errors if you forgot to implement some of the methods.



          An interface "forces" you to follow some contract; a parent class does not.
          In fact, an interface is a contract.
          Whoever decides to implement it, has to implement the contract.




          • interface = the "what" = contract = specification

          • class = the "how" = implementation






          share|improve this answer












          Yes, you could have a parent class with empty methods (or some of them implemented).. and you'd get a similar result. However, you would not get compile errors if you forgot to implement some of the methods.



          An interface "forces" you to follow some contract; a parent class does not.
          In fact, an interface is a contract.
          Whoever decides to implement it, has to implement the contract.




          • interface = the "what" = contract = specification

          • class = the "how" = implementation







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 19:30









          Luís Soares

          2,26211633




          2,26211633

























              0














              The biggest use case for interfaces in Java is to facilitate Polymorphism. Polymorphism means having a single class behave like multiple other classes. Multiple inheritance is not supported in Java so you can't extend more than one class, but you can implement multiple interfaces. There are many useful docs about this topic, here is an official one from oracle to get you started: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html



              Interfaces are also useful as a "social contract" between a designer of a class/program and the developer who implements it. By implementing an interface you promise that your implementation follows specific behaviors. This allows for consistency amongst different implementations, which is especially useful when trying to collaborate with many developers on a single, large project.






              share|improve this answer


























                0














                The biggest use case for interfaces in Java is to facilitate Polymorphism. Polymorphism means having a single class behave like multiple other classes. Multiple inheritance is not supported in Java so you can't extend more than one class, but you can implement multiple interfaces. There are many useful docs about this topic, here is an official one from oracle to get you started: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html



                Interfaces are also useful as a "social contract" between a designer of a class/program and the developer who implements it. By implementing an interface you promise that your implementation follows specific behaviors. This allows for consistency amongst different implementations, which is especially useful when trying to collaborate with many developers on a single, large project.






                share|improve this answer
























                  0












                  0








                  0






                  The biggest use case for interfaces in Java is to facilitate Polymorphism. Polymorphism means having a single class behave like multiple other classes. Multiple inheritance is not supported in Java so you can't extend more than one class, but you can implement multiple interfaces. There are many useful docs about this topic, here is an official one from oracle to get you started: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html



                  Interfaces are also useful as a "social contract" between a designer of a class/program and the developer who implements it. By implementing an interface you promise that your implementation follows specific behaviors. This allows for consistency amongst different implementations, which is especially useful when trying to collaborate with many developers on a single, large project.






                  share|improve this answer












                  The biggest use case for interfaces in Java is to facilitate Polymorphism. Polymorphism means having a single class behave like multiple other classes. Multiple inheritance is not supported in Java so you can't extend more than one class, but you can implement multiple interfaces. There are many useful docs about this topic, here is an official one from oracle to get you started: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html



                  Interfaces are also useful as a "social contract" between a designer of a class/program and the developer who implements it. By implementing an interface you promise that your implementation follows specific behaviors. This allows for consistency amongst different implementations, which is especially useful when trying to collaborate with many developers on a single, large project.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 19:33









                  NMerkl

                  414




                  414






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437001%2fim-having-difficulties-understanding-how-the-interface-function-is-useful-in%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)