简体   繁体   中英

Cannot inherit inside same package in Java

I am still learning Java and my problem is that I cannot inherit inside the same package. *Please explain in detail thanks in advance

Class 1 in package:

package Jpak02;

public class PTaccess_modes {
    public int a = 1;
    protected int b = 2;
    int c = 3;
    private int d = 4;

    PTaccess_modes() {
        System.out.println("\nInside PTaccess_mode class in package Jpak02");
        System.out.println("\nPublic int a : " + a);
        System.out.println("\nProtected int b : " + b);
        System.out.println("\nDefault int c : " + c);
        System.out.println("\nPrivate int d : " + d);
    }
}

Class 2 in same package:

package Jpak02;

class PTaccess_modes_1 extends PTaccess_modes {
    PTaccess_modes_1() {
        System.out.println("\nInside PTaccess_mode_1 class in package Jpak02");
        System.out.println("\nPublic int a : " + a);
        System.out.println("\nProtected int b : " + b);
        System.out.println("\nDefault int c : " + c);
        System.out.println("\nPrivate int d : " + d);
    }
}

Compiling errors :

      D:\softwareinstall\Java_progs\packages\Jpak02>javac PTaccess_modes_1.java
      PTaccess_modes_1.java:5: error: cannot find symbol
      class PTaccess_modes_1 extends PTaccess_modes
                                ^
        symbol: class PTaccess_modes
      PTaccess_modes_1.java:10: error: cannot find symbol
        System.out.println("\nPublic int a : "+a);
                                          ^
        symbol:   variable a
        location: class PTaccess_modes_1
      PTaccess_modes_1.java:11: error: cannot find symbol
        System.out.println("\nProtected int b : "+b);
                                             ^
        symbol:   variable b
        location: class PTaccess_modes_1
      PTaccess_modes_1.java:12: error: cannot find symbol
        System.out.println("\nDefault int c : "+c);
                                           ^
        symbol:   variable c
        location: class PTaccess_modes_1
      4 errors

I just created a new folder called Jpak02 , then inside created two files: PTaccess_modes.java and PTaccess_modes_1.java, then copied the content of your classes into them (no changes done from your current given code), and compiled in order:

D:\>javac Jpak02\PTaccess_modes.java

D:\>javac Jpak02\PTaccess_modes_1.java

When compiling the second class, I got this (expected) error:

Jpak02\PTaccess_modes_1.java:9: error: d has private access in PTaccess_modes
        System.out.println("\nPrivate int d : " + d);
                                                  ^
1 error

After this, I deleted the .class file generated by PTaccess_modes.java file and compiled it again (this time added a comment on the line using private d field):

D:\>del Jpak02\PTaccess_modes.class

D:\>javac Jpak02\PTaccess_modes_1.java

And compiled and generated both .class files (as expected).


Then, last but not least, I accessed to the package and compiled the class from there:

D:\>cd Jpak02

D:\Jpak02>javac PTaccess_modes_1.java

And now, I got the problems you stated:

PTaccess_modes_1.java:3: error: cannot find symbol
class PTaccess_modes_1 extends PTaccess_modes {
                               ^
  symbol: class PTaccess_modes
PTaccess_modes_1.java:6: error: cannot find symbol
        System.out.println("\nPublic int a : " + a);
                                                 ^
  symbol:   variable a
  location: class PTaccess_modes_1
PTaccess_modes_1.java:7: error: cannot find symbol
        System.out.println("\nProtected int b : " + b);
                                                    ^
  symbol:   variable b
  location: class PTaccess_modes_1
PTaccess_modes_1.java:8: error: cannot find symbol
        System.out.println("\nDefault int c : " + c);
                                                  ^
  symbol:   variable c
  location: class PTaccess_modes_1
4 errors

TL;DR: Make sure to compile the classes from the right location and using their complete name, this is, including the package name (as shown in the first and second block of this answer).

You can not use the variable d in the class that inherits. Its declared as private that means it can only be used in the class it was declared, no other place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM