clarification on the super call


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: clarification on the super call

  1. #1
    Join Date
    Apr 2004
    Posts
    5

    clarification on the super call

    Just a quick one, I am a beginner to OOP Java and just want to clarify the keyword super. I know that it calls the contructor of the parent class but what I want to clarify is

    Class A {

    method One

    }

    Class B extends Class A {

    super();

    }

    If class B inherits class A then it has access to class A's methods. If I had defined a method One in class B, overriding the method defined in class A by using the super keyword I understand that I would use class A's method and not the overriden version. What I want to know is must super be called all the time. If for example I do not override a method in class B must I still use super or is it implicit?

    By extending class A class B has access to method One. Is the only way to access method One to use the super call to class A's constructor or is it dependent on whether the method in class B has been overriden or not.

    Thanks a million, my exam is in two weeks and need to get to grips with this stuff now

    Cheers!

    Claire

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    if ClassB does not overide a particular method in ClassA then the method is inherited by ClassB and it is as if the method was defined in ClassB. The only time you need to use super is if you overide the method.

  3. #3
    Join Date
    Apr 2004
    Posts
    5
    Thats exactly what I needed to know thanks! I have conflicting notes, some used super when class B did not override any methods, some didn't. Thanks for clarify it.

    Regards,

    Claire

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    note that super() must be called from within a method..

    and further note that in an inherited class (actually every class is an inherited class), the call to super() is put in for you automatically by the compiler, if you do not put it in yourself.

    this leads to confusion if you have provided a constructor in the parent, then the default constructor is not applied, but the call to super() in the subclass will reference it. this causes a compile time error:

    Code:
    public class Parent{
      int age;
    
      Parent(int i){
        age = i;
      }
    }
    
    
    public class Child extends Parent{
      String name;
    
      Child(String s){
        super();
        name = s;
      }
    }
    the red bit is inserted by the compiler immediately before compilation. the compiler does not edit nor save your source file, so the following error may be confusing:

    C:\javawork\trash\Child.java:4: Parent(int) in Parent cannot be applied to ()
    Child(String s){
    ^
    1 error
    the red bit inserted by the compiler is attempting to call a default (no arguments) constructor in parent. the presence of Parent(int) constructor destroys the presence of the default Parent() constructor.
    In seeking Parent() and only finding Parent(int) the compiler throws the "cant be applied" error, and rightly so, as it doesnt know what to do.

    To resolve the error, either provide a default constructor of Parent()
    or put in a call to super(int) that will cause the known constructor of Parent(int) to be run.

    There is argument for the former, rather than the latter, as a constructor's purpose is to initialize a class to a usable state. Someone who uses your class later, may not know anything about it (other than he/she wants to extend it), so you should provide a default constructor that inits everything as it should be.. relying on someone else to correctly call super(arg, arg, arg, etc) is not so wise when they didnt write the class
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by mikeBarr81
    The only time you need to use super is if you overide the method.
    clarification:

    The only time you need to use super is if you override the method (of A) in class B, but wish to call the original method of class A, rather than the overridden method in class B. Calls of this nature may only be performed inside class B, or one of its subclasses:

    Code:
    class A{
      void methodA(){print("A.methodA");}
    }
    
    class B{
      void methodA(){print("B.methodA");} //overrides A.methodA()
    
      void anotherMethod(){
        this.methodA(); //prints B.methodA
        super.methodA();//prints A.methodA
        methodA();//prints B.methodA ('this' is implicit)
      }
    }
    
    class C{
    
      A classA = new A();
      B classB = new B();
      A actuallyB = new B(); //child can be assigned to parent type
    
      void someMethod(){
    
        classA.methodA(); //prints A.methodA
        classB.methodA(); //prints B.methodA
        //the parent type has no effect; the overridden method is run
        actuallyB.methodA(); //prints B.methodA
        //casting B into its parent type also has no effect - overridden method is run
        ((A)classB).methodA(); //prints B.methodA
    
        //attempt to access the A superclass via the B subclass (from C class)
        classB.super.methodA(); //fails (compile error)
    
      }
    }


    so, if you have a class of the child type, and youre trying to use a method on the parent type, from an external class (C in this case, is unrelated to A or B), you cannot access it's superclass, unless it explicitly exposes it:

    Code:
    class B{
    
      public A getSuper(){
        return super;
      }
    }
    then, you can call overridden methods.. but it's a form of protection, because external classes shouldnt be able to come along, and create a new child class, then ask it for its super class, and use the super class.. they should jsut create an instance of the super class in the first place.. and if they cant.. there's usually a good reason for it :)
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    true cjard, thats what I was thinking but not what I wrote. Was there really the need to rub my nose in it three times though? jk

  7. #7
    Join Date
    Feb 2004
    Posts
    808
    lol, dude.. i PROMISE, i have NO IDEA how that came to be posted 3 times.. im not normally such a tard
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  8. #8
    Join Date
    Apr 2004
    Posts
    5

    Excuse the pun you guys are "Super"

    Thanks alot. It is a lot clearer now, what was confusing was prob that the compiler will implicity include it if not already there & therefore some code examples I had called super() and others did not. It makes more sense now. Thanks for your help! claire

  9. #9
    Join Date
    Feb 2004
    Posts
    808
    welcome

    i'd personally far prefer it if the compiler edited my source file and put the actual text in there, along with a comment "added by the javac.exe compiler", because it confuses the heck out of a lot of beginners, and even me sometimes, ends up chasing the issue round for 30 minutes, before I remember...

    maybe its just cause i'm too thick to remember everything..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center