File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mock exam question.can it be complied? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mock exam question.can it be complied?" Watch "mock exam question.can it be complied?" New topic
Author

mock exam question.can it be complied?

Crystal So
Greenhorn

Joined: Nov 25, 2000
Posts: 10
without creating any object of the class, can the method be executed? I met the following question, would any one please help me out:
public class C{
public static void main(String[] args){
System.out.println("Run");
}
}
public class D extends C{
}
Once the program is compiled you execute the following from the command line.
java D
Select the result of compiling and executing the program in this way.
A.the program will not compile because D does not contain a main method.
B. the program compiles and prints Run to the system console.
C.the program compiles but doesn't print anything to the system console.
D.the program compiles but the Java interpreter gives an error when you try to run D stating that class D does not contain a main method.
the question is from the mock exam in : http://joppa.appliedreasoning.com/JavaCertification.html
yiftah frechter
Greenhorn

Joined: Nov 26, 2000
Posts: 1
The answer is B
when inherit a class(or an instance) gets all the members that are not private
note that the code of the question(class C or D) can't be in file because of the fact
that two public classes can't be in the same file.
Originally posted by Crystal So:
without creating any object of the class, can the method be executed? I met the following question, would any one please help me out:
public class C{
public static void main(String[] args){
System.out.println("Run");
}
}
public class D extends C{
}
Once the program is compiled you execute the following from the command line.
java D
Select the result of compiling and executing the program in this way.
A.the program will not compile because D does not contain a main method.
B. the program compiles and prints Run to the system console.
C.the program compiles but doesn't print anything to the system console.
D.the program compiles but the Java interpreter gives an error when you try to run D stating that class D does not contain a main method.
the question is from the mock exam in : http://joppa.appliedreasoning.com/JavaCertification.html

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

correct me if im wrong but private members are also inherited
//A.java
public class A {
private int a;
}
//B.java
public class B extends A {
A thisA = new A();
a=1; //ok
thisA.a = 1; //compile error
}


SCJP
Visit my download page
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
No the private variables cannot be overriden by subclass period!
In your example both intializations will give problems...
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i have conflicting information here. the jls says:
"A private class member or constructor is accessible only within the class body in which the member is declared and is not inherited by subclasses. "
Thinking in Java says:

When you do this, you automatically get all the data members and methods in the base class. Here�s an example:

//: c06 etergent.java
// Inheritance syntax & properties.
class Cleanser {
private String s = new String("Cleanser");
public void append(String a) { s += a; }
public void dilute() { append(" dilute()"); }
public void apply() { append(" apply()"); }
public void scrub() { append(" scrub()"); }
public void print() { System.out.println(s); }
public static void main(String[] args) {
Cleanser x = new Cleanser();
x.dilute(); x.apply(); x.scrub();
x.print();
}
}
public class Detergent extends Cleanser {
// Change a method:
public void scrub() {
append(" Detergent.scrub()");
super.scrub(); // Call base-class version
}
// Add methods to the interface:
public void foam() { append(" foam()"); }
// Test the new class:
public static void main(String[] args) {
Detergent x = new Detergent();
x.dilute();
x.apply();
x.scrub();
x.foam();
x.print();
System.out.println("Testing base class:");
Cleanser.main(args);
}
} ///:~

in this example Deteregent inherits(and calls) the public method append() which modifies the private field s
So what is right?
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
That's true the progrtam works fine, but if you see append is public so teh sublass cal call that method though it's instance but try chnaging teh value o s directly and it willl give error, as long as the method is public you don't care what it does internally. "You cannot override any private variable or method".
even though you may do it by calling public method.
I hope this makes it clear.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

I will compile and run the code tomorrow but it looks to me like there are definately 2 different instances of s.
i guess its a matter of what you mean when you say inherits.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mock exam question.can it be complied?