Results 1 to 20 of 25
- 08-10-2011, 08:25 PM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
- 08-10-2011, 08:47 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 08-10-2011, 10:23 PM #3
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 9
Access/visibility modifiers (in order of rank [highest first]):
public — Accessible from everywhere
protected — Only accessible from the same package as well as from subclasses
* — Only accessible from the same package
private — Only accessible from the same class
* is the default access modifier and is used when no access modifier is used (i.e. in absence of the keywords: public, private, protected).
* is called ”default” or ”package private”.
Due to the existence of synthetic access (ugly enough to use; it is implicitly used) private methods/classes/&c can be access from inner classes,
otherwise at least package private must be used.Ex animo! Hibernate
Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.
- 08-11-2011, 12:33 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
THANK YOU VERY MUCH FOR YOUR REPLY JosAH , Hibernate ,
Hope you are all fine by god grace .
Now coming to my doubt : practically i tried this theory by creating a class "Balance" in package called "MyPack" and i have created another class called "TestBalance" in another package called "MyPack2" but the protected members are not accessible by "TestBalance" ,so please help me and correct the error in my code below
Java Code:package MyPack; public class Balance { String name; double bal; protected Balance(String n, double b) { name = n; bal = b; } protected void show() { if(bal<0) System.out.print("--> "); System.out.println(name + ": $" + bal); } } package MyPack2; import MyPack.*; class TestBalance { public static void main(String args[]) { /* Because Balance is public, you may use Balance class and call its constructor. */ Balance test[] = new Balance[3]; test[0] = new Balance("K. J. Fielding", 123.23); test[1] = new Balance("Will Tell", 157.02); test[2] = new Balance("Tom Jackson", -12.33); for(int i=0; i<3; i++) test[i].show(); } }
Last edited by sunde887; 08-11-2011 at 01:05 PM. Reason: Switched quote tags to code tags
- 08-11-2011, 12:39 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Class TestBalance doesn't extend class Balance so it doesn't have access to its protected members.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-11-2011, 12:56 PM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
Yes JosAH , i even tried like tried extending the Balance class , but still i am getting error bro :( . can you please correct my code ? please bro i am in great confusion
package MyPack;
public class Balance {
String name;
double bal;
protected Balance(String n, double b) {
name = n;
bal = b;
}
protected void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
package MyPack2;
class TestBalance extends Balance {
public static void main(String args[]) {
Balance test[] = new Balance[3];
test[0] = new Balance("K. J. Fielding", 123.23);
test[1] = new Balance("Will Tell", 157.02);
test[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) test[i].show();
}
}
- 08-11-2011, 01:01 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
What is the exact error message? And where? For one thing: I don't see any protected data members (just methods).
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-11-2011, 01:20 PM #8
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
- 08-11-2011, 01:25 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 08-11-2011, 02:08 PM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
java.lang.NoClassDefFoundError: Balance
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Caused by: java.lang.ClassNotFoundException: Balance
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 12 more
Exception in thread "main"
- 08-11-2011, 02:20 PM #11
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
i think u missed the below statement
import MyPack.Balance;
in your TestBalance class
- 08-11-2011, 02:29 PM #12
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
ur TestBalance class should lokk like this
package MyPack2;
import MyPack.Balance;
public class TestBalance extends Balance {
protected TestBalance(String n, double b) {
super(n, b);
}
public static void main(String args[]) {
TestBalance test[] = new TestBalance[3];
test[0] = new TestBalance("",123.25);
test[1] = new TestBalance("Will Tell", 157.02);
test[2] = new TestBalance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) test[i].show();
}
}
- 08-11-2011, 02:39 PM #13
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
Thanks for the reply bro , for the above code i am getting the below error bro ,
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Balance(String, double) is not visible
The constructor Balance(String, double) is not visible
The constructor Balance(String, double) is not visible
The method show() from the type Balance is not visible
at MyPack2.TestBalance.main(TestBalance.java:14)
- 08-11-2011, 02:54 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 08-11-2011, 02:55 PM #15
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
do u see m not calling new Balance(String,Double) from main method
instead u hv to use the sub Class ie new TestBalance(String,Double)
- 08-11-2011, 03:22 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 08-11-2011, 03:46 PM #17
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
package MyPack;
public class Balance {
String name;
double bal;
protected Balance(String n, double b) {
name = n;
bal = b;
}
protected void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
package MyPack2;
import MyPack.Balance;
class TestBalance extends Balance {
protected TestBalance(String n, double b) {
super(n, b);
}
public static void main(String args[]) {
Balance test[] = new Balance[3];
test[0] = new Balance("K. J. Fielding", 123.23);
test[1] = new Balance("Will Tell", 157.02);
test[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) test[i].show();
}
}
- 08-11-2011, 03:58 PM #18
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 9
Do not construct, Balance, construct TestBalance:
Java Code:Balance test[] = new Balance[3]; test[0] = new TestBalance("K. J. Fielding", 123.23); test[1] = new TestBalance("Will Tell", 157.02); test[2] = new TestBalance("Tom Jackson", -12.33);
Java Code:TestBalance test[] = new TestBalance[3]; test[0] = new TestBalance("K. J. Fielding", 123.23); test[1] = new TestBalance("Will Tell", 157.02); test[2] = new TestBalance("Tom Jackson", -12.33);
Ex animo! Hibernate
Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.
- 08-11-2011, 04:05 PM #19
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 9
Because of show being protected only the second alternative will compile.
Edit:
Conclusion:
static members do never have access to protected members in other packages.
Edit 2:
Also:
protected constructors are only accessible by the line:
super(…);Last edited by Hibernate; 08-11-2011 at 04:14 PM.
Ex animo! Hibernate
Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.
- 08-11-2011, 09:26 PM #20
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
wooooooow you are rocking bro , now all are working without error .
Because of show being protected only the second alternative will compile.protected constructors are only accessible by the line:
super(…);thank s in advance bro ...
Similar Threads
-
Error "org.apache.commons.logging package does not exist" appeared
By vjoshi in forum Java ServletReplies: 2Last Post: 06-16-2011, 05:44 PM -
An "if" statement inside a "for" loop?
By soccermiles in forum New To JavaReplies: 18Last Post: 04-20-2010, 04:44 AM -
"package javax.microedition.lcdui doesnt exsist" But on suns webbpage they say it do!
By Addez in forum New To JavaReplies: 4Last Post: 10-30-2009, 08:54 PM -
Eclipse "import not resolved" package problem?
By spamsickle in forum New To JavaReplies: 3Last Post: 08-24-2009, 12:44 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 08:35 AM
Bookmarks