title: Abstract Class vs Interfaces with example
body: <p>So I am trying to understand the differences between Abstract Class and Interfaces. I know this is a well covered topic but I wrote some code to see if I can get a better idea of the concept. Let me know please if I am on the right track as far as how I am reasoning the differences out.</p>
  
  <p>My understanding is that I would use an abstract class when there are core similarities between objects/classes. In my example here, I use Quadrilaterals as my abstract class. This is a 4-sided figure (speaking in a two-dimensional sense). This means that I can create Rectangle and Square objects by extending this abstract class and take advantage of the 4-sidedness(?) of the abstract class. Therefore, these classes will inherit that core property. So an abstract class may primarily be used to write common code to reflect these core properties. So in essence, my abstract class DEFINES the derived classes (Rectangle and Square). Right so far?  </p>
  
  <p>The Interface is used when there are also commonalties, but the difference is that the interface, unlike the abstract class, does NOT define the class that uses it. It merely has abstract methods (contracts, in a sense) that must be used if the interface is implemented about a particular class. The purpose of this is to use common methods in somewhat similar classes to highlight similar features of those classes. Like getArea() and getPerimeter() of all Shapes, whether Quadrilateral or not. Right?</p>
  
  <p>In my example, I implemented the method getNumberofSidesForQuadrilaterals to return the number of sides for the Quadrilaterals. This will always return 4. I could have used this method in the interface instead but it would be pointless since that would result in me having to call the method on an object that we already know is 4 sided (for each 4 sided object created) Would seem like a waste of time. Am I right? close, way off? Added examples with object references (to show relationships) are welcome.</p>
  
  <pre><code>package abstractex;
  
  /**
   *
   * 
   */
  
  
   interface Shapes{     
  
      public double getPerimeter();
      public double getArea();
  
  }
  
      public abstract class Quadrilaterals implements Shapes{
  
      double base,height; 
      double sides;
  
  
      public int getNumberofSidesForQuadrilaterals(){
  
          final int sides = 4;
          System.out.println(sides);
          return sides;
      }
  
   }
  
  class Rectangle extends Quadrilaterals  {
  
      public Rectangle(double base, double height) {
          this.base = base;
          this.height = height;
      }
  
      @Override
      public double getPerimeter(){
  
          double r = 1;
  
          double perimeter = (2 * base) + (2 * height);
  
          System.out.println("The perimeter of the Rectangle is " + perimeter);
  
          return perimeter;
      }
  
      @Override
      public double getArea(){
  
          double area = base * height;
  
          System.out.println("The area of the Rectangle is " + area);
  
  
          return base * height;
      }
  
  }  
  
   class Square extends Quadrilaterals {
  
  
      public Square(double base, double height) {
          this.base = base;
          this.height = height;
      }
  
      @Override
      public double getPerimeter(){
  
          double perimeter = base * 4;
  
          if (base == height)
          {
              System.out.println("The perimeter of the Square is " + perimeter);
          } else {
              System.out.println("The base and height of the square must be equal");
          }
  
          return perimeter;
  
       }   
  
      @Override
      public double getArea(){
  
          double area = base * base;
  
           if (base == height)
          {
              System.out.println("The area of the Square is " + area);
          } else {
              System.out.println("The base and height of the square must be equal");
          }
  
          return area;
      }
  
   }
  
  class Circle implements Shapes{
  
      double radius;
  
      public Circle(double radius) {
          this.radius = radius;
      }
  
      @Override
      public double getPerimeter(){
  
          double perimeter = 2*radius*Math.PI;
  
          System.out.println("The perimeter of the Circle is " + perimeter);
  
          return perimeter;
      }
  
      @Override
      public double getArea(){
  
          double area =  Math.PI*radius*radius; 
  
          System.out.println("The area of the Circle is " + area);
          return area;
      }
  
      public static void main(String[] args) {
  
          Rectangle c = new Rectangle(5,2);
          c.getPerimeter();
          c.getArea();
          c.getNumberofSidesForQuadrilaterals();
  
  
          Square d = new Square(5,5);
          d.getPerimeter();
          d.getArea();
          d.getNumberofSidesForQuadrilaterals();
  
  
          Circle e = new Circle(5);
          e.getArea();
          e.getPerimeter();
  
  
      }
  
   } 
  </code></pre>
  
  <p>Output : </p>
  
  <p>The perimeter of the Rectangle is 14.0</p>
  
  <p>The area of the Rectangle is 10.0</p>
  
  <p>4</p>
  
  <p>The perimeter of the Square is 20.0</p>
  
  <p>The area of the Square is 25.0</p>
  
  <p>4</p>
  
  <p>The area of the Circle is 78.53981633974483</p>
  
  <p>The perimeter of the Circle is 31.41592653589793</p>