0
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 by Ezzaral: Added code tags. Please use them to format any code that you post.

3
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by stultuske
0

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.

0

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 by DavidKroukamp: n/a

0

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

1

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.

1

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 by DavidKroukamp: n/a

0

@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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.