Results 1 to 5 of 5
  1. #1
    bob
    bob is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Exclamation this() and super() ?

    could somone plz gv me an answer.?
    how to use this() and super() together in a constructor code.???
    both needs to me the first statement..!

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    In constructor either this() or super() should be the first one to get executed.

    U can't give both.

    My question is why u got the necessity to use both in ur constructor?

    Always by default constructor refers the current scope (i.e) this().why u need to call explicitly?
    Ramya:cool:

  3. #3
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    if u implemeted some abstract class u can use super() method
    otherwise use "this "
    it referes the current class
    :) javadeveloper

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by britto_bicsjohn View Post
    if u implemeted some abstract class u can use super() method
    otherwise use "this "
    it referes the current class
    I'm sorry but this reply is completely wrong. Use of super has nothing to do with abstract classes. It all has to do with whether you want your constructor to first call a specific constructor of the super class or if you wish it to call another constructor of the current class.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    My question is why u got the necessity to use both in ur constructor?

    Always by default constructor refers the current scope (i.e) this().why u need to call explicitly?
    I suppose it's possible that if the superclass has a parameterized constructor that isn't called from the default subclass constructor, but the default subclass constructor does some required initialization, a parameterized subclass constructor might want to do both, so it's tempting to think that explicit calls to both are required from the parameterized subclass constructor:
    Java Code:
    class Super {
        private int x;
        
        public Super() {
            this(10);
        }
    
        public Super(int x) {
            this.x = x;
        }
    }
    
    class Sub extends Super {
        private int y;
    
        // default constructor does local init and wants default superclass init
        public Sub() {
            y = 5;
            ...   // etc.
        }
    
        // constructor wants default local init and custom superclass init
        public Sub(int x) {
            // this() ?? we'd like this too...
            super(x);
        }
    }
    An easy solution is to abstract the default subclass initialization into a subclass method that the parameterized subclass constructor can also call.

Similar Threads

  1. super
    By dj kourampies in forum New To Java
    Replies: 5
    Last Post: 01-23-2009, 03:07 PM
  2. this() vs super()
    By Hevonen in forum New To Java
    Replies: 9
    Last Post: 12-04-2008, 01:43 PM
  3. Super CSV 1.20
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 08:22 PM
  4. Super CSV 1.15
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-16-2007, 06:37 PM
  5. Use super. or this.
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:52 AM

Posting Permissions

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