Java Access Specifier issue

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1315 Views - Last Post: 11 January 2013 - 10:53 AM Rate Topic: -----

#1 NDragger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 10-August 11

Java Access Specifier issue

Posted 10 January 2013 - 09:09 AM

package MyPack;
public class Bank 
{
	protected double bal;
	protected String name;

	public Bank(String s, double ba){
		name=s;
		bal=ba;
	}

	protected void show(){
		System.out.println("The name of the Customer is " + name + " " + "and the balance is "+bal);
	}
}



package YourPack;
class BankBal extends MyPack.Bank
{
	public static void main(String[] args) 
	{
		Bank b = new Bank("NDragger",25);
		b.show();
	}
}



show method is protected in Bank class but it must be accessible in BankBal class but i'm getting these errors

BankBal.java:2: error: constructor Bank in class Bank cannot be applied to given
 types;
class BankBal extends MyPack.Bank
^
  required: String,double
  found: no arguments
  reason: actual and formal argument lists differ in length
BankBal.java:6: error: cannot find symbol
                Bank b = new Bank("NDragger",25);
                ^
  symbol:   class Bank
  location: class BankBal
BankBal.java:6: error: cannot find symbol
                Bank b = new Bank("NDragger",25);
                             ^
  symbol:   class Bank
  location: class BankBal
3 errors



Is This A Good Question/Topic? 0
  • +

Replies To: Java Access Specifier issue

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,527
  • Joined: 12-December 12

Re: Java Access Specifier issue

Posted 10 January 2013 - 09:34 AM

Because main() is in your class I believe you need to specifically define and call (super) the parent constructor:

public class BankBal extends Bank {

    public BankBal(String s, Double ba) {
        super(s, ba);
    }
    public static void main(String[] args) {
        Bank b = new Bank("NDragger", 25);
        b.show();
    }
}


But my knowledge of Java is a little.. weak :)/>

This post has been edited by andrewsw: 10 January 2013 - 09:34 AM

Was This Post Helpful? 2
  • +
  • -

#3 NDragger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 10-August 11

Re: Java Access Specifier issue

Posted 10 January 2013 - 09:38 AM

I already tried that and it does solve the issue but i want to know why?
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Java Access Specifier issue

Posted 10 January 2013 - 09:44 AM

Because you defined a custom constructor in the base class, that is the only one that is created meaning that there exists no default parameterless constructor for the Bank class.

When you inherit from a class, you have to call some constructor in the base class. This would be fine if there was a parameterless one as Java would do it for you, but since you defined your own, Java doesn't do the work for you. Therefore you have to create a constructor in the subclass that explicitly calls the base class constructor via super.
Was This Post Helpful? 2
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,527
  • Joined: 12-December 12

Re: Java Access Specifier issue

Posted 10 January 2013 - 09:45 AM

The extended class will call the parameter-less parent-constructor, even if you haven't defined one - it invents one! You wouldn't need to use super() in this case.

But as soon as you've have created a parent-constructor (with parameters) it will no longer invent a parameterless version. So you either have to create the parameter-less constructor in the parent class, or specifically call (using super()) the relevant constructor in the parent class.
Was This Post Helpful? 1
  • +
  • -

#6 NDragger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 10-August 11

Re: Java Access Specifier issue

Posted 10 January 2013 - 10:04 AM

View PostRyano121, on 10 January 2013 - 04:44 PM, said:

Because you defined a custom constructor in the base class, that is the only one that is created meaning that there exists no default parameterless constructor for the Bank class.

When you inherit from a class, you have to call some constructor in the base class. This would be fine if there was a parameterless one as Java would do it for you, but since you defined your own, Java doesn't do the work for you. Therefore you have to create a constructor in the subclass that explicitly calls the base class constructor via super.


I did the following changes according to what i understood but i got the following error.

changes i did

package MyPack;
public class Bank 
{
	protected double bal;
	protected String name;

	public void data(String s, double ba){
		name=s;
		bal=ba;
	}

	protected void show(){
		System.out.println("The name of the Customer is " + name + " " + "and the balance is "+bal);
	}
}



package YourPack;
class BankBal extends MyPack.Bank
{
	public static void main(String[] args) 
	{
		Bank b = new Bank();
        b.data("NDragger",25);
		b.show();
	}
}



Error i got

BankBal.java:6: error: cannot find symbol
                Bank b = new Bank();
                ^
  symbol:   class Bank
  location: class BankBal
BankBal.java:6: error: cannot find symbol
                Bank b = new Bank();
                             ^
  symbol:   class Bank
  location: class BankBal
2 errors


by the way parameter constructor isn't a call to the base class?
why does it needs a parameter less constructor?
Thanks :)
Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Java Access Specifier issue

Posted 10 January 2013 - 10:51 AM

I think you have misunderstood slightly.

In this case there is no point in having a default parameterless constructor as you want to set the values of the fields when you create the object. Therefore the Bank class makes perfect sense.

The problem lies in the BankBal class. Say I create a BankBal object. How can I say what I want the values of bal and name to be? Currently I can't so there is an error.

To solve it you 1. need to do what andrewsw did and define a constructor in the derived class which calls super with the parameters, or define a parameterless constructor in the base class that sets default values to the fields e.g

public Bank() {
   bal = 0;
   name = "default name";
}


That will make the compiler happy as the subclass implicitly calls the parameters super constructor when you don't define any yourself.

Hope that helps
Was This Post Helpful? 2
  • +
  • -

#8 NDragger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 10-August 11

Re: Java Access Specifier issue

Posted 10 January 2013 - 10:57 PM

What i understood is, to avoid error while creating the Object of BankBal it is necessary to initialize the two variables.

The bank class has now two constructors one parameter less and second paremetrized but i'm still getting those errors.

I have YourPack dir with BankBal and inside YourPack dir MyPack dir with Bank.

any help?

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#9 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Java Access Specifier issue

Posted 10 January 2013 - 11:20 PM

Well, can you please post your updated code after the changes you have made?...it would certainly help in finding out your problem.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Java Access Specifier issue

Posted 11 January 2013 - 04:41 AM

Your 2 classes are in two different packages: MyPack and YourPack (by the way by conventions, packages name are all in lowercase letter)

For a class to see the constructors, methods, variables of another class in another package these have to be declared public not protected.

protected is to be accessed by any class in the same package.

So for your main() method in yourpack to see the show() method in mypack: show has to be declared public
Was This Post Helpful? 2
  • +
  • -

#11 NDragger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 10-August 11

Re: Java Access Specifier issue

Posted 11 January 2013 - 05:09 AM

If i'm not wrong, protected variables and methods are accessible by sub-classes from different packages.

so the show() method must be accessible by the BankBal class because it inherits from Bank class.

Thanks for the response :)
Was This Post Helpful? 1
  • +
  • -

#12 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Java Access Specifier issue

Posted 11 January 2013 - 05:11 AM

I think it's best if you post your code with the updates you've done to it along with any problems/error messages you are having so we can clear this all up.
Was This Post Helpful? 0
  • +
  • -

#13 NDragger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 10-August 11

Re: Java Access Specifier issue

Posted 11 January 2013 - 05:24 AM

Changes i did

package MyPack;
public class Bank 
{
	protected double bal;
	protected String name;
  
   public Bank(){
	   name = "default";
	   bal = 0;
   }

	public Bank(String s, double ba){
		name=s;
		bal=ba;
	}

	protected void show(){
		System.out.println("The name of the Customer is " + name + " " + "and the balance is "+bal);
	}
}



package YourPack;
class BankBal extends MyPack.Bank
{
	public static void main(String[] args) 
	{
		Bank b = new Bank("NDragger",25);
		b.show();
	}
}



Errors

BankBal.java:6: error: cannot find symbol
                Bank b = new Bank();
                ^
  symbol:   class Bank
  location: class BankBal
BankBal.java:6: error: cannot find symbol
                Bank b = new Bank();
                             ^
  symbol:   class Bank
  location: class BankBal
2 errors


Was This Post Helpful? 0
  • +
  • -

#14 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Java Access Specifier issue

Posted 11 January 2013 - 05:31 AM

You need to import MyPack.Bank; to fix those errors. However as pbl said you will get another error on the .show() in main as it needs to be public.
Was This Post Helpful? 1
  • +
  • -

#15 NDragger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 10-August 11

Re: Java Access Specifier issue

Posted 11 January 2013 - 05:41 AM

oh man! new error i got

Exception in thread "main" java.lang.NoClassDefFoundError: BankBal (wrong name:
YourPack/BankBal)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)

This post has been edited by NDragger: 11 January 2013 - 05:56 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2