• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Queries Again...

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!


1.What this program will print?

Class Base{
int value=0;
Base(){
addValue();
}
void addValue(){
value+=10;
}
Class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value+=20;
}
}
public Class Test{
public static void main(String{}args){
Base b= new Derived();
System.out.println(b.getValue());
}

1.10
2.20
3.30
4.40

Answer given :4 i.e 40
I feel 20 because b is pointing derived class , moreover values is not a static variable
What is the concept here??

2.

What will happen if you try to compile this..
interface A{

public void innerMeth();
}
public class Test{
A a;
int memVar=1;
void aMethod(){
a=new A(){
public void innerMeth(){
System.out.println(memVar);
}};
}
public static void main(String []args){
Test t= new Test();
t.a.innerMeth();
}}

Answer Given: Null Pointer Exception
Why??

Cheers..
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Smitha,

It would have been better if you post two questions in seperate posts. I will try to answer both.

Originally posted by Smitha Amey Ballikar:
Hi!!


1.What this program will print?

Class Base{
int value=0;
Base(){
addValue();
}
void addValue(){
value+=10;
}
Class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value+=20;
}
}

public Class Test{
public static void main(String{}args){
Base b= new Derived();
System.out.println(b.getValue());
}

1.10
2.20
3.30
4.40

Answer given :4 i.e 40
I feel 20 because b is pointing derived class , moreover values is not a static variable
What is the concept here??


Are you sure you typed the code correctly. I do not see getValue() defined. I feel it should not compile.

And your second Q

Originally posted by Smitha Amey Ballikar:



2.

What will happen if you try to compile this..
interface A{

public void innerMeth();
}
public class Test{
A a;
int memVar=1;
void aMethod(){
a=new A(){
public void innerMeth(){
System.out.println(memVar);
}};
}
public static void main(String []args){
Test t= new Test();
t.a.innerMeth();
}}

Answer Given: Null Pointer Exception
Why??

Cheers..


The inner class is defined inside aMethod() method and you are not calling this method from main. So a is not assigned any object, hence the Null Pointer Exception.

Hope I explained correctly.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

if you are new to the forum then this is a brief reminder to PLEASE post all

your code within the tabs so that you make it more readable. aiming

for a SCJP its very important for us to abide by the Java coding rules.

cheers,
amar
[ August 05, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Eeven though base class refers to derived class.. it executes derived class method only. thats is callled dynamic method dispatch. so in this case ans is 40 since it executes derived class consutructor only.

lemme know.. if dosen't answers to ur queries..

--sekhar
 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am not clear here about the 1st question.As sekhar says derived class method is called.
The method says:
value+=20 which is value=value +20
here intial value of value is 0.
so effectively
value=0+20 =20
so how come value becomes 40..

Please explain!!
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Smitha Amey Ballikar:
Hi,

I am not clear here about the 1st question.As sekhar says derived class method is called.
The method says:
value+=20 which is value=value +20
here intial value of value is 0.
so effectively
value=0+20 =20
so how come value becomes 40..

Please explain!!



It because addValue() gets called twice, once in the Base constructor and once in the Derived constructor. Since Derived extends Base, the constructor for Base has a hidden super() call which causes addValue() to be called. And then the actual constructor for Derived() also calls addValue(), hence you get 40.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first example won't compile due to multiple errors, so none of the four answers is correct. Here are some of the errors:

  • All three classes should be declared with the lowercase keyword class, not the capitalized word Class
  • main(String{}args) should be main(String[] args) with square brackets
  • The closing brace for the Base class is missing
  • The closing brace for the Test class is missing
  • b.getValue() refers to a getValue() method that is not defined in either Base or Derived

  • If you fixed all these errors and decided to put a getValue() method in both Base and Derived, and made them both return value, you'd have this code:

    You can run this yourself and see that it does print 40.

    Here's why it prints 40. Let's walk through the program.

    The main method begins
    A reference to a Base, called b, is declared.
    A new object is created by using new and calling the constructor of Derived.
    The constructor of Derived has an implicit first instruction to call the constructor of its superclass, Base.
    The Base constructor begins execution by calling the constructor of Object (the super class of Base) and by initializing the instance variable of Base to 0.
    The next statement in the constructor of Base executes, which is a call to the addValue() method of the this object, which is actually a Derived object, so the addValue() method of the Derived class is the one that executes.
    The addValue() method of the Derived class increases the object's instance variable value to 20, and the method returns.
    The Base class constructor completes.
    The Derived class constructor continues with its next statement, which is a call to the addValue() method of the this object, which is an instance of Derived, so the method that executes is again the addValue() method of the Derived class.
    The addValue() method of the Derived class increases the object's instance variable value by 20 yet again, increasing it to 40. Then the addValue() method returns.
    The constructor of the Derived class finishes execution, and the object is fully created.
    Back int the main method of Test, the Base variable b is assigned a value that makes it a reference to the Derived object we just finished creating, that has an instance variable called value that is currently set to 40.
    b.getValue() returns the value of the instance variable called value, which is 40.
    System.out.println prints that value to standard out.

    The two tricky parts to notice are:
    1. When the constructor of Derived executes, it calls super() as its implicit first statement
    2. When the constructor of Base executes and calls the object's addValue() instance method, that method is selected based on the type of the this object, which is a Derived, not a Base.
     
    Smitha Ballikar
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Joseph..

    Your explanation was too good.I understood the concept now.
    Thanks for it!!!

    Cheers..
     
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No wonder you cleared SCJP with 98%. Your explanation is interpretted the code very well.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic