9 Replies Latest reply on Jul 1, 2010 2:29 PM by 3004

    About Abstract Class

    843789
      abstract class MyAbstract
      {
           abstract void m();
           
      }

      public class MyClass extends MyAbstract
      {
           void m()
           {
                System.out.println("my abstract ");
           }
           
           void m1()
           {
                System.out.println("my sub class ");
           }
           

           public static void main(String[] args)
           {
                MyAbstract my=new MyClass ();
                my.m1(); //Line 5
           
           }

      }



      consider line  5  for compilation error.  what the reason  ?
      But if i call m instead of m1 (my.m)display the result is
      my abstract..  can u please help m e ?

      Basically  we can't create instance for Abstract Class.




        • 1. Re: About Abstract Class
          3004
          The reason you can't call my.m1() has nothing to do with abstract classes. It's because my is a reference to MyAbstract, and that only has method m(). Method m1() is declared on MyClass. The fact that the my reference variable happens to point to a MyClass object is irrelevant. It's declared as type MyAbstract, so you can only call MyAbstract's methods (unless you cast it, and if you do, then that's a red flag to look for a design flaw).
          • 2. Re: About Abstract Class
            796262
            consider line  5  for compilation error.  what the reason  ?
            The compilation error (which, for some reason, you haven't posted) probably already tells you that.
            But if i call m instead of m1 (my.m)display the result is
            my abstract..  can u please help m e ?
            What?
            Basically  we can't create instance for Abstract Class.
            True enough. But that's not the problem here. Consider this case:
            public class AnimalTester{
               public class Animal{
                  public void eat(){
                     System.out.println("An animal is eating.");
                   }
               }
            
               public class Cat extends Animal{
                  public void eat(){
                     System.out.println("A cat is eating.");
                  }
            
                  public void meow(){
                     System.out.println("Meow!");
                  }
               }
            
               public static void main(String [] args){
                  Animal animal = new Cat();
                  animal.meow(); //we don't know this is a Cat, so we can't meow
               }
            }
            That will result in the same compiler error that I assume you're getting. It has nothing to do with abstract classes. Does that make more sense?
            • 3. Re: About Abstract Class
              843789
              ya that' good you r correct.. refernce belongs super class .



              what the deference

              Superclass instancename=new Superclass ();

              and

              Superclass instancename=new Subclass ();


              usually what happen when use new
              • 4. This Thread is now moved
                darrylburke
                Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
                • 5. Re: About Abstract Class
                  699554
                  Mrjavan wrote:
                  ya that' good you r correct.. refernce belongs super class .



                  what the deference

                  Superclass instancename=new Superclass ();

                  and

                  Superclass instancename=new Subclass ();


                  usually what happen when use new
                  [Read this tutorial|http://java.sun.com/docs/books/tutorial/java/concepts/index.html], focus on Object, Classes, Inheritance and Interfaces.

                  Mel
                  • 6. Re: About Abstract Class
                    Herko_ter_Horst
                    Mrjavan wrote:
                    abstract class MyAbstract
                    {
                         abstract void m();
                         
                    }

                    public class MyClass extends MyAbstract
                    {
                         void m()
                         {
                              System.out.println("my abstract ");
                         }
                         
                         void m1()
                         {
                              System.out.println("my sub class ");
                         }
                         

                         public static void main(String[] args)
                         {
                              MyAbstract my=new MyClass ();
                              my.m1(); //Line 5
                         
                         }

                    }



                    consider line  5  for compilation error.  what the reason  ?
                    But if i call m instead of m1 (my.m)display the result is
                    my abstract..  can u please help m e ?
                    You can't call m1() through a reference of type MyAbstract, because m1() is not a method known to MyAbstract. m1() is a method of MyClass.
                    Basically  we can't create instance for Abstract Class.
                    You can (and you have) created an instance of a subclass of an abstract class and assigned it to a variable of the abstract type.
                    • 7. Re: About Abstract Class
                      3004
                      Mrjavan wrote:
                      ya that' good you r correct.. refernce belongs super class .



                      what the deference

                      Superclass instancename=new Superclass ();

                      and

                      Superclass instancename=new Subclass ();
                      First, that's not an instance name. Instances don't have names. Varaibles do. In both cases, you have a variable of type reference-to-Superclass. So if you're going to stick a non-null value into that variable, that value has to be a reference that points to an object that IS-A Superclass. Since Any child class of Superclass also IS-A Superclass, that variable can also hold a reference to a child.

                      In the first line, you create an instance of Superclass and put a reference to it into the variable.

                      In the second line, you create an instance of Subclass and put a reference to it into the variable.
                      usually what happen when use new
                      Is that a question? I don't even know what you're asking. Every time we use new, the same thing happens: A new object is created and a reference to it is returned. (Or else an exception is thrown.)
                      • 8. Re: About Abstract Class
                        843789
                        Hi


                        I accept " Abstract Class can not be instatiated ". I am did n't. Here

                        MyAbstract my=new MyClass ();//

                        I am create instance for MyClass not MyAbstract , but reference belongs to  MyAbbstract

                        so MyAbstract reference don't know about MyClass method.

                        that's why compilation error my.m1();* because m1() not a method of MyAbstract.

                        But your example is good . Thanks
                        ...................Mr javan
                        • 9. Re: About Abstract Class
                          3004
                          Mrjavan wrote:
                          I am create instance for MyClass not MyAbstract , but reference belongs to  MyAbbstract

                          so MyAbstract reference don't know about MyClass method.

                          that's why compilation error my.m1();* because m1() not a method of MyAbstract.
                          This is what I told you in reply 1.