Results 1 to 4 of 4
  1. #1
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Abstract class implemented

    Hello,

    I can't figure out why this works.

    If I have a class without constructor that implements an abstract class (without constructor), why I can instantiate it?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Abstract class implemented

    To get your terminology right, you don't implement an abstract class, you *extend* it. Implements is only used for interfaces.

    Second, if you don't have a constructor in your class, the compiler will create a default constructor, the one with no parameters, automatically. This has nothing to do with abstract classes. As long as your super class doesn't have a constructor (so also a default constructor), you're fine.

    Things change when the super class has a constructor. Then your class needs to define one as well, *and* call one of these.

    Example of something that compiles:
    Java Code:
    public abstract class Blah {
        
        //nothing happens here
        
        abstract void myMethod();
    }
    
    public class Foobar extends Blah {
    
        @Override
        void myMethod() {
        }
    }
    This example doesn't compile:
    Java Code:
    public abstract class Blah {
    
        public Blah(String s) {
            
        }
    
        abstract void myMethod();
    }
    
    public class Foobar extends Blah {
    
        @Override
        void myMethod() {
        }
    }
    The compile now complains: No default constructor available in Blah.

  3. #3
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Abstract class implemented

    Thanks for the explanation. So, in that case I can make this Foobar x = new Foobar();

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Abstract class implemented

    Rule of thumb: you can only use the default constructor (the one without parameters: new Foobar() ) when there are *NO* constructors at all, or when you have defined it explicitly. As soon as there is a constructor with parameters, the default constructor won't be automatically created anymore, and you would have to create one yourself.

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2013, 03:53 AM
  2. Replies: 4
    Last Post: 04-14-2011, 12:42 PM
  3. How should an Animation class be implemented?
    By kiregad in forum New To Java
    Replies: 9
    Last Post: 06-18-2010, 08:39 AM
  4. Replies: 6
    Last Post: 02-11-2009, 11:51 AM
  5. Class Reflection: Finding implemented interfaces
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •