Results 1 to 3 of 3
Thread: rule for using the super keyword
- 10-01-2010, 07:09 AM #1
- 10-01-2010, 08:04 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
- 10-01-2010, 09:02 AM #3
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
-
Problem with navigation rule!
By ulix83 in forum JavaServer Faces (JSF)Replies: 2Last Post: 01-14-2011, 01:48 PM -
calling variable using super super..
By Stephen Douglas in forum New To JavaReplies: 7Last Post: 08-16-2010, 06:12 AM -
Identifier Naming Rule
By udayadas in forum Advanced JavaReplies: 1Last Post: 08-21-2008, 05:56 PM -
New rule to PMD-ECLIPSE plugin
By jai in forum EclipseReplies: 1Last Post: 04-21-2008, 05:40 PM -
Java Rule engines
By bluekswing in forum Advanced JavaReplies: 0Last Post: 01-02-2008, 10:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks