I have this two classes and its giving me an error when i compile it that cannot access BaseClass.

package pckage1;

class BaseClass {

    public int x = 10;
    private int y = 10;
    protected int z = 10;
    int a = 10; //Implicit Default Access Modifier
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    private int getY() {
        return y;
    }
    private void setY(int y) {
        this.y = y;
    }
    protected int getZ() {
        return z;
    }
    protected void setZ(int z) {
        this.z = z;
    }
    int getA() {
        return a;
    }
    void setA(int a) {
        this.a = a;
    }
}

public class SubclassInSamePackage extends BaseClass {

    public static void main(String args[]) {
        BaseClass rr = new BaseClass();
        rr.z = 0;
        SubclassInSamePackage subClassObj = new SubclassInSamePackage();
        //Access Modifiers - Public
        System.out.println("Value of x is : " + subClassObj.x);
        subClassObj.setX(20);
        System.out.println("Value of x is : " + subClassObj.x);
        //Access Modifiers - Public
        //      If we remove the comments it would result in a compilaton
        //      error as the fields and methods being accessed are private
        /*      System.out.println("Value of y is : "+subClassObj.y);

         subClassObj.setY(20);

         System.out.println("Value of y is : "+subClassObj.y);*/
        //Access Modifiers - Protected
        System.out.println("Value of z is : " + subClassObj.z);
        subClassObj.setZ(30);
        System.out.println("Value of z is : " + subClassObj.z);
        //Access Modifiers - Default
        System.out.println("Value of x is : " + subClassObj.a);
        subClassObj.setA(20);
        System.out.println("Value of x is : " + subClassObj.a);
    }
}

How are you executing the compiler? What is the classpath when you execute the javac command.
Where are the source files?

What is the exact error message, and exactly which line does it refer to?

package pckage1;
class BaseClass {
public int x = 10;
private int y = 10;
protected int z = 10;
int a = 10; //Implicit Default Access Modifier
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
private int getY() {
return y;
}
private void setY(int y) {
this.y = y;
}
protected int getZ() {
return z;
}
protected void setZ(int z) {
this.z = z;
}
int getA() {
return a;
}
void setA(int a) {
this.a = a;
}
}
public class SubclassInSamePackage extends BaseClass {
public static void main(String args[]) {
BaseClass rr = new BaseClass();
rr.z = 0;
SubclassInSamePackage subClassObj = new SubclassInSamePackage();
//Access Modifiers - Public
System.out.println("Value of x is : " + subClassObj.x);
subClassObj.setX(20);
System.out.println("Value of x is : " + subClassObj.x);
//Access Modifiers - Public
// If we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are private
/* System.out.println("Value of y is : "+subClassObj.y);
subClassObj.setY(20);
System.out.println("Value of y is : "+subClassObj.y);*/
//Access Modifiers - Protected
System.out.println("Value of z is : " + subClassObj.z);
subClassObj.setZ(30);
System.out.println("Value of z is : " + subClassObj.z);
//Access Modifiers - Default
System.out.println("Value of x is : " + subClassObj.a);
subClassObj.setA(20);
System.out.println("Value of x is : " + subClassObj.a);
}
}

Hmm yes please give the full error message the compiler is giving you, because it seems fine on mine?

do i have to select on a package or somethin..?

or do i have to put it in one class?

public class SubclassInSamePackage extends BaseClass { //error:cannot access BaseClass

Can you post the full text of the compiler's error message?

public class SubclassInSamePackage extends BaseClass {

//error:cannot access BaseClass

public static void main(String args[]) {
BaseClass rr = new BaseClass();
rr.z = 0;
SubclassInSamePackage subClassObj = new SubclassInSamePackage();
//Access Modifiers - Public
System.out.println("Value of x is : " + subClassObj.x);
subClassObj.setX(20);
System.out.println("Value of x is : " + subClassObj.x);
//Access Modifiers - Public
// If we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are private
/* System.out.println("Value of y is : "+subClassObj.y);
subClassObj.setY(20);
System.out.println("Value of y is : "+subClassObj.y);*/
//Access Modifiers - Protected
System.out.println("Value of z is : " + subClassObj.z);
subClassObj.setZ(30);
System.out.println("Value of z is : " + subClassObj.z);
//Access Modifiers - Default
System.out.println("Value of x is : " + subClassObj.a);
subClassObj.setA(20);
System.out.println("Value of x is : " + subClassObj.a);
}
}

public class SubclassInSamePackage extends BaseClass {
public static void main(String args[]) {
BaseClass rr = new BaseClass();
rr.z = 0;
SubclassInSamePackage subClassObj = new SubclassInSamePackage();

//Access Modifiers - Public
System.out.println("Value of x is : " + subClassObj.x);
subClassObj.setX(20);
System.out.println("Value of x is : " + subClassObj.x);
//Access Modifiers - Public
// If we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are private
/* System.out.println("Value of y is : "+subClassObj.y);
subClassObj.setY(20);
System.out.println("Value of y is : "+subClassObj.y);*/
//Access Modifiers - Protected
System.out.println("Value of z is : " + subClassObj.z);
subClassObj.setZ(30);
System.out.println("Value of z is : " + subClassObj.z);
//Access Modifiers - Default
System.out.println("Value of x is : " + subClassObj.a);
subClassObj.setA(20);
System.out.println("Value of x is : " + subClassObj.a);
}
}

The error is next to the heading...

Please post the ** full text ** of the error message.
Here is a sample:

TestCode6.java:295: cannot access InPackage
 bad class file: .\InPackage.class
 class file contains wrong class: ForumQuestions6.InPackage
 Please remove or make sure it appears in the correct subdirectory of the classpath.
 class Testing extends InPackage {}
                    ^

**

Error : Main method not found in class pckage1.Baseclass, Please define the main method as : public static void main (String [] args ) #
**

I don't know that a main() method is required by the compiler. You do not have to have a main() method in a class.
That error must be from your IDE. I have no idea how your IDE works, I use the javac and java commands for compiling and executing java programs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.