14 Replies Latest reply on Feb 14, 2007 6:05 PM by 3004

    Abstract Classes

    807599
      Hi,

      If i define a class as abstract (public abstract class myClass)

      Does this implicitly make all the methods in the class abstract or do i need to define them as abstract.

      Also in java if a method is abstract, can a derived class use the method without modifying / defining the method


      Many Thanks
      Alex
        • 1. Re: Abstract Classes
          800435
          Hi,

          If i define a class as abstract (public abstract
          class myClass)

          Does this implicitly make all the methods in the
          class abstract or do i need to define them as
          abstract.
          You can have abstract methods or not in one abstract class, but if you have a method abstract in a "normal" class you must declare that class as abstract
          >
          Also in java if a method is abstract, can a derived
          class use the method without modifying / defining the
          method
          yes


          regards,

          Manuel Leiria
          • 2. Re: Abstract Classes
            807599
            Thanks for your help

            So if I say my class is abstract, do i still need to label my methods as abstract (do you even label them as abstract) or is it implicitly labled if the class is labled as abstract


            Cheers
            Alex
            • 3. Re: Abstract Classes
              800435
              Thanks for your help

              So if I say my class is abstract, do i still need to
              label my methods as abstract (do you even label them
              as abstract) or is it implicitly labled if the class
              is labled as abstract


              Cheers
              Alex
              you must label your abstract methods as abstract (if you don't, you'll get a compile error). You know an abstract method doesn't have a body, don't you?
              • 4. Re: Abstract Classes
                807599
                Thanks for your help

                So if I say my class is abstract, do i still need to
                label my methods as abstract (do you even label them
                as abstract) or is it implicitly labled if the class
                is labled as abstract


                Cheers
                Alex
                in an abstract class, you must mark abstract methods as such, and you must not mark concrete methods as abstract. the above answer is slightly misleading as it implies one can have abstract methods in a concrete (normal??) class. you can't. having abstract methods on a class that isn't declared as abstract is a syntax error and won't compile

                interfaces have only abstract methods, but you don't need to mark them as such. all methods on an interface are implicitly both public and abstract
                • 5. Re: Abstract Classes
                  807599
                  Thanks fOR yOUR HELP GUYS

                  I think ou both cleared my problem up.

                  Thanks Again
                  -Alex

                  Edit: Just checking, but you cant directly create an instance of an abstract class can you? Even if there are some methods inside that are not abstract. Also is every method in java implicitly defined as virtual if not otherwise said

                  Message was edited by:
                  alexcurtis
                  • 6. Re: Abstract Classes
                    807599
                    Hi,

                    If i define a class as abstract (public abstract
                    class myClass)

                    Does this implicitly make all the methods in the
                    class abstract or do i need to define them as
                    abstract.
                    No not implicitly, you have to declare one or more methods abstract manually/expicitly. You can have an abstract class w/o abstract methods in it, but it still can't be directly implemented.
                    Also in java if a method is abstract, can a derived
                    class use the method without modifying / defining the
                    method

                    Use, no. Leave alone and not implement, yes ... but then both that method and that class would also need to be declared abstract.
                    Many Thanks
                    Alex
                    • 7. Re: Abstract Classes
                      807599
                      Thanks fOR yOUR HELP GUYS

                      I think ou both cleared my problem up.

                      Thanks Again
                      -Alex

                      Edit: Just checking, but you cant directly create an
                      instance of an abstract class can you? Even if there
                      are some methods inside that are not abstract. Also
                      is every method in java implicitly defined as virtual
                      if not otherwise said

                      Message was edited by:
                      alexcurtis
                      nope, no instances of abstract classes. ever. all methods in java are virtual. there's no way to mark them otherwise
                      • 8. Re: Abstract Classes
                        807599
                        With some "what if" questions you can simple try it and see. Here is a demo:
                        abstract class A {
                            public abstract void f();
                        
                            public void g() {
                                System.out.println("A.g");
                            }
                        }
                        
                        abstract class B extends A {
                            public void h() {
                                System.out.println("B.h");
                            }
                        }
                        
                        class C extends B {
                            public void f() {
                                System.out.println("C.f");
                            }
                        }
                        One thing you seem to be confused about is when a method in a class is abstract. That's easy: if a method has a body (like g, above) it is not abstract. If a method declared in a class has no body (= is abstract) then you must declare it with the keyword "abstract" (like f in class A). It's a simple as that.

                        If a class has an abstract method, either declared in it (like f in class A) or inherited by it (like f in class B) then that class is abstract and must be declared with the "abstract" keyword. An abstract class may have non-abstract methods, too, like g in class A -- this is common.

                        One more thing: a class can be declared as abstract even if it has no abstract methods:
                        abstract class D {
                        }
                        Two examples of this in the Java API are java.awt.Component and javax.swing.JComponent.
                        • 9. Re: Abstract Classes
                          807599
                          You can't instance a abstract class without abstract methods... but you can simulate an empty subclass to do this, but it has not sense:
                          abstract class A{
                              public int x;
                              
                              public A(){
                                  x=0;
                              }
                              
                              public int increment(){
                                  return x++;
                              }
                          
                              public static void main(String[] args) throws Exception {
                                  A x=new A(){}; //Empty subclass
                              }
                          }
                          • 10. Re: Abstract Classes
                            807599
                            You can't instance a abstract class without abstract
                            methods... but you can simulate an empty subclass to
                            do this, but it has not sense:
                            abstract class A{
                            public int x;
                            
                            public A(){
                            x=0;
                            }
                            
                            public int increment(){
                            return x++;
                            
                            public static void main(String[] args) throws
                            Exception {
                            A x=new A(){}; //Empty subclass
                            }
                            that's not a simulation. it's an anonymous inner class
                            • 11. Re: Abstract Classes
                              807599
                              You can't instance a abstract class without
                              abstract
                              methods... but you can simulate an empty subclass
                              to
                              do this, but it has not sense:
                              abstract class A{
                              public int x;
                              
                              public A(){
                              x=0;
                              }
                              
                              public int increment(){
                              return x++;
                              
                              public static void main(String[] args) throws
                              Exception {
                              A x=new A(){}; //Empty subclass
                              }
                              that's not a simulation. it's an anonymous inner class
                              Yes, with an anonymous inner class you can simulate that you are instancing a object of class A. The problem is that if A is abstract it must have a reason.
                              • 12. Re: Abstract Classes
                                807599
                                You can't instance a abstract class without
                                abstract
                                methods... but you can simulate an empty
                                subclass
                                to
                                do this, but it has not sense:
                                abstract class A{
                                public int x;
                                
                                public A(){
                                x=0;
                                }
                                
                                public int increment(){
                                return x++;
                                
                                public static void main(String[] args) throws
                                Exception {
                                A x=new A(){}; //Empty subclass
                                }
                                that's not a simulation. it's an anonymous inner
                                class

                                Yes, with an anonymous inner class you can simulate
                                that you are instancing a object of class A. The
                                problem is that if A is abstract it must have a
                                reason.
                                why is it a "simulation" of anything? it's a straightforward subclass of A
                                • 13. Re: Abstract Classes
                                  3004
                                  that's not a simulation. it's an anonymous inner
                                  class

                                  Yes, with an anonymous inner class you can simulate
                                  that you are instancing a object of class A.
                                  Nope. You're instantiating a concrete subclass of A. It's no different than a named subclass, other than the fact that you can't refer to it by name. You're not "simulating" anything.
                                  • 14. Re: Abstract Classes
                                    3004
                                    If i define a class as abstract (public abstract
                                    class myClass)

                                    Does this implicitly make all the methods in the
                                    class abstract or do i need to define them as
                                    abstract.
                                    No that's what an interface does. The purpose of an abstract class is to be able to have both abstract and concrete methods. In principle the compiler could deduct that a method is abstract but it doesn't. In abstract classes you have to explicitly declare abstract methods.
                                    Also in java if a method is abstract, can a derived
                                    class use the method without modifying / defining the
                                    method
                                    Yes, as soon as an abstract method has been defined it can be used. The reason is that there's no way it can actually be called without having first been implemented. This is ensured by the fact that objects can only be instantiated from concrete (fully implemented) classes.