The Artima Developer Community
Sponsored Link

Java Buzz Forum
Friday Java Quiz: protected

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Weiqi Gao

Posts: 1808
Nickname: weiqigao
Registered: Jun, 2003

Weiqi Gao is a Java programmer.
Friday Java Quiz: protected Posted: Aug 8, 2008 8:49 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Weiqi Gao.
Original Post: Friday Java Quiz: protected
Feed Title: Weiqi Gao's Weblog
Feed URL: http://www.weiqigao.com/blog/rss.xml
Feed Description: Sharing My Experience...
Latest Java Buzz Posts
Latest Java Buzz Posts by Weiqi Gao
Latest Posts From Weiqi Gao's Weblog

Advertisement

Brian Goetz made the following claim on the openjfx-compiler project mailing list:

Brian Goetz: In fact, I'll bet that many java developers would be surprised to learn what "protected" really means in Java. Even language experts get confused on these points.

So what does protected really mean? Let's find out if you know the answer.

Q: Will the following Java source files compile together? Will Main run without exceptions? If so, what will be printed?

// base/Base.java
package base;
public class Base {
  protected int i;
  public Base(int i) {
    this.i = i;
  }
}
// derived/Derived.java
package derived;
import base.*;
public class Derived extends Base {
  public Derived(int i) {
    super(i);
  }
  public void foo(Base b) {
    System.out.println(b.i);
  }
}
// Main.java
public class Main {
  public static void main(String[] args) {
    Derived d = new Derived(1024);
    d.foo(d);
  }
}

Today's semi-relaxed rules: no running the compiler, but you can consult the JLS.

Read: Friday Java Quiz: protected

Topic: MyEclipse on Sale in Europe Previous Topic   Next Topic Topic: MyEclipse Job Growth: 2200%

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use