java's abstract class concept

Discussion in 'Programming' started by lewisb, Apr 23, 2005.

  1. #1
    A class such as Number, which represents an abstract concept and should not be instantiated, is called an abstract class. An abstract class is a class that can only be subclassed-- it cannot be instantiated.

    To declare that your class is an abstract class, use the keyword abstract before the class keyword in your class declaration:

    abstract class Number {
    . . .
    }

    If you attempt to instantiate an abstract class, the compiler displays an error similar to the following and refuses to compile your program:
    AbstractTest.java:6: class AbstractTest is an abstract class.
    It can't be instantiated.
    new AbstractTest();
    ^
    1 error

    Abstract Methods
    An abstract class may contain abstract methods, that is, methods with no implementation. In this way, an abstract class can define a complete programming interface, thereby providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface. However, the abstract class can leave some or all of the implementation details of those methods up to its subclasses.
     
    lewisb, Apr 23, 2005 IP
  2. davedx

    davedx Peon

    Messages:
    429
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    :eek:

    You get some bizarre posts on these forums...
     
    davedx, Apr 28, 2005 IP
  3. sn3twork

    sn3twork Active Member

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    I never understood why they have abstracts AND interfaces...
     
    sn3twork, Oct 26, 2009 IP
  4. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #4
    Abstract java class is called interface.
     
    it career, Oct 28, 2009 IP
  5. sn3twork

    sn3twork Active Member

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #5
    Oh really then why is it that abstract class methods can have implementations, but interface method's cannot?
     
    sn3twork, Oct 28, 2009 IP
  6. sn3twork

    sn3twork Active Member

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #6
    Also, you make it sound as if they are one entity.. they aren't SUN developers for some reason split them up.

    you can have a

     
    
    abstract class helloworld
    {
    
      public void hello()
      {
         System.out.println("Hello World");
      }
    
    }
    
    
    Code (markup):
    or you can have a

     
    
    interface helloworld
    {
    
      public void hello();
    
    }
    
    
    Code (markup):
     
    sn3twork, Oct 28, 2009 IP
  7. nomzz

    nomzz Well-Known Member

    Messages:
    475
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    110
    #7
    There are some major differences between abstract and interfaces.

    Java prodive multiple implementations through interfaces, Interfaces can be implemented multiple times by a class and can be extended by interface multiple times but abstract can be extended only ones... .

    Absract can be taken as alternative when you need partial things.

    Some other points:

    - class should implement all methods of interface where as in abstract not necessary.

    - An abstract class can have complete or incomplete methods where as interfaces can have only signature of methods.

    - Interfaces cant implement methods like abstract classes; like abstract class may contain variables, contructors, methods.

    Hope this helps.

    Cheers!!
     
    nomzz, Oct 28, 2009 IP
  8. sn3twork

    sn3twork Active Member

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #8
    Those aren't major differences..
    First, You say that interfaces can be implemented multiple times by a class and classes can be extended by and interface multiple times. As far as I know you can overload methods no matter what... and why the hell would you put implents interface1, interface1, interface1....

    Second, abstract classes should be all that exist since you can choose whether or not to implement the methods in the abstract class definition and if you choose not to guess what? It's an interface under a different name.




     
    sn3twork, Oct 29, 2009 IP
  9. nomzz

    nomzz Well-Known Member

    Messages:
    475
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    110
    #9
    ok to rephrase multiple interfaces not multiple times.... typo error.

    Your second point is not valid every situation is different and if you have read design patterns then you cank now.

    Interface is just definations thats it so how can be abstract is different name of interface?

    Please if you have your own differences point them and those are the valid difference which i just said it helps alot while making a wireframe.. I dont know why you said but if you know other differences please mention.

    thanks
     
    nomzz, Oct 29, 2009 IP
  10. sn3twork

    sn3twork Active Member

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #10
    You said an interface is just definitions.. I can make an abstract class without any implementations.. therefore just definitions.. so an abstract class is an interface.. with the capability to add implementations if needed.
     
    sn3twork, Oct 30, 2009 IP
  11. be@stie

    be@stie Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #11
    :D Since when?
     
    be@stie, Oct 30, 2009 IP
  12. sn3twork

    sn3twork Active Member

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #12
    The lack of understanding here is overwhelming :p
     
    sn3twork, Oct 31, 2009 IP
  13. nomzz

    nomzz Well-Known Member

    Messages:
    475
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    110
    #13
    If you want its your choice but not from sun developers...you cant do this in interface i dont know why you cant just understand and why you dont want to differentiate both these entities? any thing wrong or by doing your achieving some new thing in object oriented or your trying to say they had less knowledge while making these concepts than you ?

    If you want to add a value in discussion then research first other wise its pointless.
     
    nomzz, Oct 31, 2009 IP
  14. sn3twork

    sn3twork Active Member

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #14
    Are you trying to tell me US congress has more knowledge than me when they make laws?
     
    sn3twork, Nov 3, 2009 IP