Results 1 to 3 of 3
  1. #1
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    160
    Rep Power
    5

    Default rule for using the super keyword

    hi
    what are the rule for using the super keyword
    do that be the first in any and where it be in the class(child) please explain in detail with example

    thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javastuden View Post
    hi
    what are the rule for using the super keyword
    do that be the first in any and where it be in the class(child) please explain in detail with example
    Every text book explains it in great detail. Do you have one? If so, read it.

    kind regards,

    Jos

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by javastuden View Post
    please explain in detail with example

    ok, i wrote a small example that shows two rules of super. the first rule is concerning the constructors and the second rule is concerning how to refer variables with the same name. look at the comments in the code.


    Java Code:
    class Parent {
    	private String name;
    	public int nr;
    
    	public Parent(String n) {
    		this.name = n;
    		nr = 10;
    	}
    }
    
    public class Child extends Parent {
    	// since the Parent has no default constructor
    	// the first statement in the constructor of the Child class must
    	// be a call to the non default constructor
    	// otherwise you will get an error like
    	// "Constructor call must be the first statement in a constructor"
    	public Child(String n, int nr) {
    		super(n);
    		this.nr = nr;
    	}
    
    	// the Child class has a variable with the same name like
    	// its parent, so to refer the parent variable use super and
    	// the current variable use this
    	private int nr;
    
    	public int getParentNr() {
    		// super refer the Parent Object
    		return super.nr;
    	}
    
    	public int getChildNr() {
    		// this means the current object
    		return this.nr;
    	}
    
    	public static void main(String[] args) {
    		Child c = new Child("Child", 5);
    		System.out.println("from the Parent class nr = " + c.getParentNr());
    		System.out.println("from the child class nr = " + c.getChildNr());
    	}
    }]

    i hope, you can use the code as starting point for super.

Similar Threads

  1. Problem with navigation rule!
    By ulix83 in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 01-14-2011, 01:48 PM
  2. calling variable using super super..
    By Stephen Douglas in forum New To Java
    Replies: 7
    Last Post: 08-16-2010, 06:12 AM
  3. Identifier Naming Rule
    By udayadas in forum Advanced Java
    Replies: 1
    Last Post: 08-21-2008, 05:56 PM
  4. New rule to PMD-ECLIPSE plugin
    By jai in forum Eclipse
    Replies: 1
    Last Post: 04-21-2008, 05:40 PM
  5. Java Rule engines
    By bluekswing in forum Advanced Java
    Replies: 0
    Last Post: 01-02-2008, 10: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
  •