thinkingmonster
thinkingmonster

Reputation: 5413

java basics related to inheritance

I am new to java below question may sound to be stupid but i am not able to figure it out as what went wrong.

below is the program i have written.

public class AccessDemo1{
    public int a_pub=2;
    private int a_pri=3;
    int a_def=4;
    protected int a_pro=5;

    Public AccessDemo1(){
        System.out.println("Value of a_pub = " + a_pub);
        System.out.println("Value of a_pri = " + a_pri);
        System.out.println("Value of a_def = " + a_def);
        System.out.println("Value of a_pro = " + a_pro);
    }
}

//package p1;

public class AccessDemo2 extends AccessDemo1{

    AccessDemo2(){
    }

    Public AccessDemo2(){
        System.out.println("Value of a_pub = " + a_pub);
        System.out.println("Value of a_pri = " + a_pri);
        System.out.println("Value of a_def = " + a_def);
        System.out.println("Value of a_pro = " + a_pro);
    }
}

//package p1;

public class demomain{

    public static void main(String args[]){

        AccessDemo2 ob1 = new AccessDemo2();

    }

}

On compiling the demomain class it give s me below error.Why so

demomain.java:7: error: cannot find symbol
AccessDemo2 ob1 = new AccessDemo2();

So after going through all the suggestion i made below changes. i break the code in to three diffrent files each holding one class as shown below.

file AccessDemo1.java with below content

package p1;

public class AccessDemo1{
    public int a_pub=2;
    private int a_pri=3;
    int a_def=4;
    protected int a_pro=5;

    public AccessDemo1(){
        System.out.println("Value of a_pub = " + a_pub);
        System.out.println("Value of a_pri = " + a_pri);
        System.out.println("Value of a_def = " + a_def);
        System.out.println("Value of a_pro = " + a_pro);
    }
}

File AccessDemo2.java with below content

package p1;

public class AccessDemo2 extends AccessDemo1{

    public AccessDemo2(){
        System.out.println("Value of a_pub = " + a_pub);
        //System.out.println("Value of a_pri = " + a_pri);
        System.out.println("Value of a_def = " + a_def);
        System.out.println("Value of a_pro = " + a_pro);
    }
}

and last file like demomain.java

package p1;

public class demomain{

    public static void main(String args[]){

        AccessDemo2 ob1 = new AccessDemo2();

    }

}

Now next problem is that i am able to compile and run the code if i am commenting the package statement in all the files.But if i include that as shown in above examples then on compiling the demomain.java i am getting below errors.

D:\javastudy>javac demomain.java
demomain.java:7: error: cannot find symbol
AccessDemo2 ob1 = new AccessDemo2();
^
  symbol:   class AccessDemo2
  location: class demomain
demomain.java:7: error: cannot find symbol
AccessDemo2 ob1 = new AccessDemo2();
                      ^
  symbol:   class AccessDemo2
  location:

 class demomain
2 errors

What is the possible reason for this is there some other way of compiling the files that include package statement ?Please guide me

Upvotes: 0

Views: 122

Answers (2)

Sanjeev
Sanjeev

Reputation: 9946

It seams problem is not with your code, it is with the folder/package structure you are using

D:\javastudy>javac demomain.java

This command tells me that you do not have any package named p1 physically created. This is the reason that you are able to run your code without package statement but not with package statement.

In order to fix the package issue you need to create a folder under javastudy named p1 (as this is your package name) and move all three classes to that folder.And then try to compile using :

D:\javastudy>javac p1\demomain.java

And run is using:

D:\javastudy>java p1.demomain

Hope this helps.

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79807

Well, you seem to have two constructors with the same signature for AccessDemo2. Also, you have misspelt public twice. Remove the first constructor

AccessDemo2(){
}

since you already have another one. And change Public to public everywhere.

Upvotes: 1

Related Questions