Results 1 to 16 of 16
  1. #1
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default General Discussion on Abstract

    Hello All,

    I started this thread just to discuss general Java topics. I am not asking the question i just wanted to share knowledge and want to discuss with all members to get more ideas. It think you will like this.

    Your Comments are Welcome.

    So let's Start.

    Todays Topic : Abstract in Java

    1) In which scenario one should go for Abstract Class rather than Interface.
    2) Can Abstract class have contractor? If yes then when it is called? and can it be private?
    3) Where we can use abstract keyword in Java(like before class declaration,variable declaration etc.).




    sanjeev,संजीव

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sanjeevtarar View Post

    1) In which scenario one should go for Abstract Class rather than Interface.
    I use interface rather than abstract, if I can see that something is change frequently. I mean If you want to make changes. may be in design, make them as interface. Abstract class may have default implementation. That is because, abstract class only allowed to subclasses.

    Quote Originally Posted by sanjeevtarar View Post
    2) Can Abstract class have contractor? If yes then when it is called? and can it be private?

    Abstract class can have constructor and, it can be private.



    Quote Originally Posted by sanjeevtarar View Post
    3) Where we can use abstract keyword in Java(like before class declaration,variable declaration etc.).
    What you mean here? Looking how to define an abstract class?

    Java Code:
    abstract public class testClass() {
    
    }

  3. #3
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default



    Quote Originally Posted by Eranga View Post


    Abstract class can have constructor and, it can be private.
    When it is called?? and are you sure that it can be private.??
    Please do not try first give the answer.

    What you mean here? Looking how to define an abstract class?

    Java Code:
    abstract public class testClass() {
    
    }
    No dear, where else .......Abstract keyword is used.



    Thanks for your reply...but i want other members reply too.:)

    Last edited by sanjeevtarar; 04-28-2008 at 11:52 AM.
    sanjeev,संजीव

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sanjeevtarar View Post


    Thanks for your reply...but i want other members reply too.:)

    Until that any comments ;)

    I can see that, it can be private bold. Are you interesting on it, right...

  5. #5
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Until that any comments ;)

    I can see that, it can be private bold. Are you interesting on it, right...
    Ok let me know one thing when abstract class constructor is called.
    Because we never instantiate abstract class.

    and then think about private constructor. and give some clarification on that.
    sanjeev,संजीव

  6. #6
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hello Eranga,

    You haven't answer....
    sanjeev,संजीव

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Why would you ever make a private constructor that just sounds stupid. Also I use abstract when I want to extend something. like lets say I have a customer generator. Then I want to have a uniform customer generator, and a bell curve customer generator. If I made the customer generator an interface there would be a lot of dupliction of code whereas if I make customer generator class abstract then uniform would have to just have decide when to create a customer and not have to worry about actually generating a customer.
    My IP address is 127.0.0.1

  8. #8
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Zosden View Post
    Why would you ever make a private constructor that just sounds stupid. Also I use abstract when I want to extend something. like lets say I have a customer generator. Then I want to have a uniform customer generator, and a bell curve customer generator. If I made the customer generator an interface there would be a lot of dupliction of code whereas if I make customer generator class abstract then uniform would have to just have decide when to create a customer and not have to worry about actually generating a customer.
    There is no use of private constructor in Abstract class. There is no use of abstract class until or unless it is extended. And if one make a private constructor then there will be no use of abstract class.....

    THEN WHY COMPILER PERMITS TO MAKE PRIVATE CONSTRUCTOR...IS THERE ANY SCENARIO WHERE IT IS USED IN ABSTRACT CLASS.
    sanjeev,संजीव

  9. #9
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    The compiler allows private constructors if you want to have a class make an object inside itself and only inside its self
    My IP address is 127.0.0.1

  10. #10
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Zosden View Post
    The compiler allows private constructors if you want to have a class make an object inside itself and only inside its self
    Then what is the use of there .... making Object Inside....
    sanjeev,संजीव

  11. #11
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Here is this topic in another forum Topic
    My IP address is 127.0.0.1

  12. #12
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Zosden View Post
    Here is this topic in another forum Topic
    Zosden, I know it very well and i was just discussing here to share/get some knowledge on that.
    sanjeev,संजीव

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sanjeevtarar View Post
    Hello Eranga,

    You haven't answer....
    Ok, just check these three classes.

    Java Code:
    abstract class AbstractClass {
        protected AbstractClass() {
            System.out.println("Abstract class constructor call.");
        }
        
        public abstract void distinctMethod();
        
        public void ususalMethod() {
          System.out.println("A ususal method is call.");
          distinctMethod();
        }
    }
    Java Code:
    public class AbstractDemo {
        public static void main(String args[]) {
           AbstractClass ref;
        
           //ref = new AbstractClass() {
    
        
           ref = new DeafultClass();
           ref.ususalMethod();
        }
    }
    Java Code:
    class DeafultClass extends AbstractClass{
        public void distinctMethod() {
          System.out.println("Another method is call.");
        }
    }

  14. #14
    Jesdisciple is offline Member
    Join Date
    May 2008
    Posts
    22
    Rep Power
    0

    Default

    I can understand how an abstract class might use a package/protected/public constructor (if it can be called by super()), but I guess private+abstract constructors are unreasonable... But would anyone ever put a private constructor in an abstract class, even on accident?

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  16. #16
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Jesdisciple View Post
    I can understand how an abstract class might use a package/protected/public constructor (if it can be called by super()), but I guess private+abstract constructors are unreasonable... But would anyone ever put a private constructor in an abstract class, even on accident?
    exactly Jesdisciple ..... There is no use of private constructor in abstract class.
    sanjeev,संजीव

Similar Threads

  1. Can this be done using abstract class??
    By romina in forum New To Java
    Replies: 6
    Last Post: 03-02-2009, 12:46 PM
  2. Replies: 6
    Last Post: 10-27-2008, 12:25 AM
  3. Abstract Class
    By gapper in forum New To Java
    Replies: 4
    Last Post: 01-31-2008, 01:25 PM
  4. GUI for Abstract...
    By judepereira in forum AWT / Swing
    Replies: 0
    Last Post: 01-04-2008, 08:40 AM
  5. How to set General options in NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 08-02-2007, 12:11 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
  •