• 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

two simple ques--please help

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please look at these ques---and what will the output be
1) what will this give if b1=b2=b3=b4=true
(b1 | b2 & b3 ^ b4) ??

2) class Z extends Y
{
String s="this";
void display(String s)
{
System.out.println("this" +s);
}
public static void main(String args[])
{
new Z();
}
Z()
{
super.display(s);
}
}
class Y
{
String s="super";
void display(String s)
{
System.out.println("super" +s);
}
}
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by preeti dengri:
please look at these ques---and what will the output be
1) what will this give if b1=b2=b3=b4=true
(b1 | b2 & b3 ^ b4) ??

2) class Z extends Y
{
String s="this";
void display(String s)
{
System.out.println("this" +s);
}
public static void main(String args[])
{
new Z();
}
Z()
{
super.display(s);
}
}
class Y
{
String s="super";
void display(String s)
{
System.out.println("super" +s);
}
}


first one true.precedence follows as &,^,| .so since b1 is true so u should directly conclude the answer to be true.
Now if b1 is not true then b2&b3 result in true and ^ing with b4 results in false and |ng with b1 results in true or false depending wether b1 is true or false

The second one will print superthis.
It is simple to understand.U are calling the function in superclass and passing the parameter s of the subclass.That shadows the parameter s in the superclass.To avoid that u can write the superclass print ststement as
void display(String s)
{
System.out.println("super" +this.s);
}
Check out the results.!!!
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by preeti dengri:
please look at these ques---and what will the output be
1) what will this give if b1=b2=b3=b4=true
(b1 | b2 & b3 ^ b4) ??

2) class Z extends Y
{
String s="this";
void display(String s)
{
System.out.println("this" +s);
}
public static void main(String args[])
{
new Z();
}
Z()
{
super.display(s);
}
}
class Y
{
String s="super";
void display(String s)
{
System.out.println("super" +s);
}
}


Hello,
#1. (b1 | b2 & b3 ^ b4) = true;
Order of precedence &, ^, and then |. So the expression is evakuated as (b1 | ( (b2 & b3) ^ b4)). Since b1 is true the result will therefore be true.
#2. The answer will be "superthis". First, void display(String s) in Z, subclass of Y, will override void display (String s) in Y, the superclass. When you intantiate an object Z, it will call its constructor and invoke super.display(s) (i.e. Y.display(s)) where s is the subclass Z's constant string "this", which is passed to the superclass' instance method display(). This instance method will then print "super" and the value of the argument instead of its own String s. This the answer would be "superthis."
Regards,
Lam
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the above post since the subclass has a constructor a superclass should have a no argument constructor or a call to super() is requiredin the subclass ?
o.k.or i am missing something ...
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nachiket,
If a class does not have a constructor, a default no argument constructor is provided implicitly for them.
The first line of any constructor must be either a call to a superclass constructor ( super() or super( "params" ) ) or the current class constructor ( this() or this( "params" ) ). Again, if an explicit call to either of these is not provided, a call to super() is implicitly provided.
Hope this clears up your concerns about the example,
-Nate
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic