Java inheritance and encapsulation

Hello. I’m learning Java using this online editor

And have tried practicing encapsulation and inheritance. My code is as follows

class Student {
    private String name; 
    public void setName(String newName);
        name = newName;
    }
    public String getName(){
        return name
    }
}

public class Michael extends Student  {
    public static void main(String[] args){
        Michael.setName("Mike");
       System.out.println(Michael.getName());
    }
}

It should be obvious what I’m trying to do here, I’m just trying to set the name of “Michael” in the name method, then print it to the console.

The error returned is as follows

/Michael.java:4: error: <identifier> expected
        name = newName;
            ^
/Michael.java:6: error: class, interface, enum, or record expected
    public String getName(){
           ^
2 errors

What’s going on here? Also, what is this online editor lacking that gets in the way whenever I try running very simple code like this example?

I think you meant

this.name = newName;

And

return this.name;

1 Like

The code looks clear, but the compiler points out what seems like working code as having problems. The issue actually is somewhere else, and the compiler is confused and points to the following code.

The issue is actually this part:

public void setName(String newName);
        name = newName;
    }

the method ends with ; rather than {,thus resulting in Java thinking name = newName is not on a method so its floating, and the getName method to be out of the class entirely!

2 Likes

Thank you. Now I have

class Student {
    private String name; 
    public void setName(String newName){
        this.name = newName;
    }
    public String getName(){
        return this.name;
    }
}

public class Michael extends Student  {
    public static void main(String[] args){
        Michael.setName("Mike");
       System.out.println(Michael.getName());
    }
}

Although the error is now

/Michael.java:13: error: non-static method setName(String) cannot be referenced from a static context
        Michael.setName("Mike");
               ^
/Michael.java:14: error: non-static method getName() cannot be referenced from a static context
       System.out.println(Michael.getName());
                                 ^
2 errors

“Cannot be referred from a static context”?

Michael is a class.
You need to make an object of type Michael first.
Or call this.setName

Ah silly me, should’ve been obvious. Thanks for your response

class Human {
    private String name; 
    public void setName(String newName){
        this.name = newName;
    }
    public String getName(){
        return this.name;
    }
}

public class Adult extends Human{
    public static void main(String[] args){
        
        Adult Michael = new Adult();
        Michael.setName("Mike");
        
       System.out.println(Michael.getName());
    }
}

Just leaving this here for archive purposes.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.