0

Hi I started to learn Java today and have a problem with my code. I have 3 different classes and want that the int "blz" gets the value of the super number and after that the value of "blz" should get printed in the main class.

Here is my current code: my main class "Start":

public class Start
{
    public static void main(String[] args)
    {
        Volksbank aufruf = new Volksbank(); //dont know how I can print blz
    }
}

my second class "Bank":

public abstract class Bank {
    private int Kontonummer;
    private int blz;

    public Bank(){}

    public Bank(int bankleitzahl){
         this.blz = bankleitzahl;
    }
} 

my last class "Volksbank":

public class Volksbank extends Bank
{
    public Volksbank(int bankleitzahl)
    {
        super(234345);
    }
}
Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
Angry Red Panda
  • 419
  • 1
  • 5
  • 28

6 Answers6

2

You need to add a "getter" to your Bank class:

public abstract class Bank {
    private int Kontonummer;
    private int blz;

    public Bank(){}

    public Bank(int bankleitzahl){
        this.blz = bankleitzahl;
    }

    public int getBlz()
    {
        return blz;
    }
}

public class Volksbank extends Bank
{
    public Volksbank(int bankleitzahl)
    {
        super(bankleitzahl);
    }
}

And then call your getBlz() method in your main:

public class Start
{
    public static void main(String[] args)
    {
        Volksbank aufruf = new Volksbank(234345);
        System.out.println(aufruf.getBlz());
    }
}
javatutorial
  • 1,916
  • 15
  • 20
  • Hey, thanks for your answer, but when I try to execute your code it says that the constructor Volksbank(); is still undefined, how do I fix that? – Angry Red Panda Oct 22 '15 at 12:10
  • @CaptainTreibholz: answer edited. You need to pass an argument to your Volksbank constructor, that's the reason for that error. :) – javatutorial Oct 22 '15 at 12:19
1

Use the pattern getter/setter and prefer using the abstract class as the left type to guarantee the contract agreement.

public class Start
{
    public static void main(String[] args)
    {
        Bank aufruf = new Volksbank(); //dont know how I can print blz
        System.out.println(aufruf.getBlz());
    }
}


    public abstract class Bank
    {
        private int Kontonummer;
        private int blz;

        public Bank(){}

        public Bank(int bankleitzahl)
        {
             this.blz = bankleitzahl;
        }

        public int getBlz(){
            return blz;
        }
     } 
Lucas
  • 129
  • 9
1

You couldn't access it because it's a private only the class in which it is declared can see it.

So you have to declare public getter for that variable in order to get it's value

public abstract class Bank
{
    private int Kontonummer;
    private int blz;

    public Bank(){}

    public Bank(int bankleitzahl)
    {
        this.blz = bankleitzahl;
    }

    public int getBlz() //public getter to access blz value
    {
        return this.blz;
    }
 }

You can access it via it's getter:-

public class Start
{
    public static void main(String[] args)
    {
        Volksbank aufruf = new Volksbank(234345);
        System.out.println(aufruf.getBlz());
    }
}

If you want to set it's value you have to declare setter for that variable

karim mohsen
  • 2,164
  • 1
  • 15
  • 19
0

You need to expose blz to outside classes first.

public abstract class Bank
{
    private int Kontonummer;
    private int blz;

    public Bank(){}

    public Bank(int bankleitzahl)
    {
        this.blz = bankleitzahl;
    }

    public int getBlz()
    {
        return this.blz;
    }
 }

Then you can get it and print it from Start:

public class Start
{
    public static void main(String[] args)
    {
        Volksbank aufruf = new Volksbank();
        System.out.println(aufruf.getBlz());
    }
}
Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

define a method called

public int getBlz() {
     return blz;
}

in super class.

public class Start
{
    public static void main(String[] args)
    {
        Volksbank aufruf = new Volksbank(); //dont know how I can print blz
        System.out.println(aufruf.getBlz());
    }
}
Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
0

crate a method called getblz() which will return the value of blz. since you are beginner I am not sure you have idea about getter or setter method. I can see you are trying to learn inheritance ,abstract classes.

To know more about getter and setter

How do getters and setters work?

Java: Getters and Setters

one more thing I want to mention that you have parametrize constructor in Volksbank. but when you are creating object of Volksbank in main you should pass int parameter while creating object. though if you dont pass jvm will create default constructor for you.

public class Start {

 public static void main(String[] args) {
  
     {
         Volksbank aufruf = new Volksbank(100); 
         System.out.println("Tha value of BLZ is"+aufruf.getblz()); 
       
         
     }
 }

}

 abstract class Bank
{
    private int Kontonummer;
    private int blz;

    public Bank(){}

    public Bank(int bankleitzahl)
    {
         this.blz = bankleitzahl;
         System.out.println("blz :"+blz);
    }
    
    public int getblz()
    {
     return blz;
    }
    
 }

  class Volksbank extends Bank
 {
     public Volksbank(int bankleitzahl)
     {
     
         super(234345);
    
         System.out.println("bankleitzahl :"+bankleitzahl);
     }
     
   
 }
Community
  • 1
  • 1
Varun
  • 4,342
  • 19
  • 84
  • 119