public final class SavingsAccount implements Serializable {

   /**
   * This constructor requires all fields to be passed as parameters.
   *
   * @param aFirstName contains only letters, spaces, and apostrophes.
   * @param aLastName contains only letters, spaces, and apostrophes.
   * @param aAccountNumber is non-negative.
   * @param aDateOpened has a non-negative number of milliseconds.
   */
   public SavingsAccount (
     String aFirstName, String aLastName, int aAccountNumber, Date aDateOpened
   ){
      super();
      setFirstName(aFirstName);
      setLastName(aLastName);
      setAccountNumber(aAccountNumber);
      //make a defensive copy of the mutable Date passed to the constructor
      setDateOpened( new Date(aDateOpened.getTime()) );
      //there is no need here to call validateState.
   }

   public SavingsAccount () {
     this ("FirstName", "LastName", 0, new Date(System.currentTimeMillis()));
   }

   public final String getFirstName() {
     return fFirstName;
   }

   public final String getLastName(){
     return fLastName;
   }

   public final int getAccountNumber() {
     return fAccountNumber;
   }

The Savings Account constructor in the above snippet has the super() keyword but the class does not extend any super class it just implments the Serializable class?
IS this correct JAVA?

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

no.
whether you put the super(); call there or not, every single class extends Object, whether you specify it or not.
super(); is just a call you make to the default constructor of the super class, in this case, the Object class.

So the super() keyword is used to extend the Object Class?

it basically calls the super classes method with the same name.

i.e.: if this method is in a class called SomeClass and it's been inherited from the class SomeOtherClass, then this method calls the functionality of the SomeOtherClass.someMethod()

this is called overloading as well. in this case this inherited method does some extra tasks than the parent's method. this key word helps to reduce typing the parent's code again in the child (very handy especially when you don't have paren't source code)

Edited 4 Years Ago by DavidKroukamp: n/a

but see there is no extends keyword to get inherited from any class except the Object class as by default the all classes inherit the Object class so the super() keyword is there to get object class members

no. your class automatically inherits everything in Object, it's the entire basis of the java language.

in this case, the super() call might even be redundant.

I'll give you a small example:

public class SuperTest{
  public SuperTest(){
    System.out.println("a");
  }
  public SuperTest(String text){
    System.out.println(text);
  }
}
public class SubTest{
  public SubTest(String input){
    System.out.println(input);    
  }

  public static void main(String[] args){
    SubTest a = new SubTest("my text");
  }
}

now, try the above code again, but changing it like this:

public SubTest(String input){
    super();
    System.out.println(input);    
  }

and now this:

public SubTest(String input){
    super(input);
    System.out.println(input);    
  }

when you've done this, just try and explain here what you think the difference is.

no. your class automatically inherits everything in Object, it's the entire basis of the java language.

in this case, the super() call might even be redundant.

I'll give you a small example:

public class SuperTest{
  public SuperTest(){
    System.out.println("a");
  }
  public SuperTest(String text){
    System.out.println(text);
  }
}
public class SubTest{
  public SubTest(String input){
    System.out.println(input);    
  }

  public static void main(String[] args){
    SubTest a = new SubTest("my text");
  }
}

now, try the above code again, but changing it like this:

public SubTest(String input){
    super();
    System.out.println(input);    
  }

and now this:

public SubTest(String input){
    super(input);
    System.out.println(input);    
  }

when you've done this, just try and explain here what you think the difference is.

@stultuske: Your example is great and therefore i have my reservations about saying this but here goes. shouldnt you SubTest class extend your SuperTest class? or the code will give errors, especially when: super(input); and it just wont display right text when super();

Edited 4 Years Ago by DavidKroukamp: n/a

@stultuske: Your example is great and therefore i have my reservations about saying this but here goes. shouldnt you SubTest class extend your SuperTest class? or the code will give errors, especially when: super(input); and it just wont display right text when super();

damn .. nyes, it is supposed to extend SuperTest (hence the classnames)
my bad, was a long day :)

This article has been dead for over six months. Start a new discussion instead.