loading...

One Small Thing

jcowie profile image JCowie ・1 min read

Great News!

I'm starting a weekly newsletter covering all things happening in the programming world.

Subscribe Now So that you don't miss the first issue!

Click Here

Wait

Whoever is the first person to comment the output of this code wins premium access to my newsletters!

The language is java by the way :)

class Base {
    public String name = "Base";
}
class Derived extends Base {
    private final String name = "Derived";
    public String getName() {
        return name;
    }
}
public class Details {
    public static void main(String[] args) {
        System.out.println(new Derived().getName());
        System.out.println(new Derived().name);
    }
}

Discussion

markdown guide
 
Main.java:21: error: name has private access in Derived
        System.out.println(new Derived().name);
 

My guess is that it doesn't compile because you can't acces the private String name on the second print.

 

Yes! Correct, a wee trick question there.

 

My guess (haven't coded in Java for a while) is:

Derived
Base

 

I am not expert in Java, output would be

Derived
Derived

I guess :)

 
 

Derived
Exception

But it wont compile