7 Replies Latest reply: Dec 30, 2010 8:22 AM by 803129 RSS

    Abstract classes

    803129
      Hi all,
      I have just read a section on abstract classes and have gone through an example using a shape class and square sub class. Ok, so I undertstand the concept that a findArea function i.e. its an abstract method as it is generic so should be defined under the superclass BUT for this demo it doesn't actually seem to be used, am I correct? this can be seen by simply removing the "public abstract double findArea();" and the code still works.........am finding it diffcult to apply this concept to reality
      public abstract class Shape {
      
          private String colour="red";
      
          Shape() {
              System.out.println("Shape's constructor called");
          }
          public String getColour() {
              return colour;
          }
      
          public void setColur(String c) {
              colour=c;
          }
      
          public String toString(){
              return "Colour is:"+ colour;
          }
      
          public abstract double findArea();
      } 
      public class Square extends Shape{
      
          private double length;
      
          /** Creates a new instance of Square */
          public Square(double l) {
              length=l;
              System.out.println("Square's constructor is called");
          }
      
          public String toString(){
              String tmp=super.toString();
              //return the information using the super class method
              tmp=tmp+"\nShape is square";//add more information
              tmp=tmp+"\nArea is "+findArea();
              return tmp;
          }
      
          public double findArea(){
              return length*length;
          }
      
      
          public static void main(String [] args){
              Square sq = new Square(5);
              System.out.println(sq.toString());
          }
      
      }
        • 1. Re: Abstract classes
          forumKid2
          You are overriding findArea() in the Square class, so it calls that method. If you remove it from the Square class it will then call findArea() in the Shape class
          • 2. Re: Abstract classes
            803129
            Just removed the findArea from the square class but it now highlights the "Square" word on the first line of Square class statement with message:

            square is not abstract and does not override abstract method findArea()
            • 3. Re: Abstract classes
              Kayaman
              I'd say the example itself is a bit poor, since as you noticed, it doesn't matter whether findArea is defined in the abstract class or just the subclass.

              A better example would be writing
                  public static void main(String [] args){
                      Shape sq = new Square(5);
                      System.out.println(sq.findArea());
                  }
              Since in that case the inheritance has some effect (and findArea needs to be defined in the abstract superclass for this to function correctly).
              • 4. Re: Abstract classes
                forumKid2
                Ahh, I didn't look good enough at your code to see that you were defining the method in the Shape class.

                Basically in the shape class you define this:
                public abstract double findArea();
                So any class that extends Shape will need to have a findArea() method. By removing the method declaration from the Shape class, you remove that restriction. The findArea() in your Square class is still perfectly valid...although it's not overriding anything at that point and your @Override annotation should show an error since you are not actually overriding anything any longer.

                Edited by: ForumKid2 on Dec 30, 2010 5:26 AM
                • 5. Re: Abstract classes
                  803129
                  Yes you are right when I remove this there is no overriding anymore ForumKid2 and to kayman when I replace sq.toString with sq.findArea in the print statement this behaviour is also true but when I wish to inherit the abstract method, it seems this is not allowed as I still get the same message as left on a previous post in that the Square is not abstract! So I guess this is the whole idea behind abstract inheritance.

                  Also, a slightly different question, I noticed that both constructors are called for Shape and Square even though the object instantiated for Square has an argument of 5 BUT the constructor of Shape is a no-arg constructor? Surely they should match for it to print i.e. there should be a:
                   public Shape (double l) 
                  rather than just a
                   public Shape() 
                  ??
                  • 6. Re: Abstract classes
                    Kayaman
                    No, a constructor, by default calls super(); i.e. the superclass no-arg constructor. If you need a different constructor to be called (or there is no no-arg constructor), you need to specify it explicitly.
                    • 7. Re: Abstract classes
                      803129
                      Ah okay....thanks and thanks for all the other posts