Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Doubt on some rules from stydy guide

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
While preparing for SCJp1.4,i was going thru a study guide, which has listed out some important points.I had some doubts regarding two rules and tried to develop an example code based on that rule.I got different output plz help on the following two doubts:

1.Overloading, Overriding, Runtime Type and Object Orientation

-If a call to the super class constructor is made with argument i.e for example with the following prototype,
super(ArgList);

Here the ArgList can be static variables or static methods belonging to the given class or one of its super class.
� ArgList cannot refer to instance variables or instance methods belonging to the given class or to one of its super class.



Now i tested the above rule by the following example , but this example is contradicting this rule, please look carefully into the code and compare it with the rule.

class Super{

Super(){} //no-arg constructor

Super(int i){ //one-arg constructor

System.out.println(i);

System.out.println("super");
}
}

public class Test extends Super{

int k=3; //no-static instance variable

Test(){} //no-arg constructor

Test(int a){ //one-arg constructor

super(a);

System.out.println("sub");}

public static void main(String a[]){

Test t = new Test();

Test t1 = new Test(t.k);
}
}//end of sub class

OUTPUT- 3, super, sub

2.Declarations and Access control

-While equating array reference variables if the dimensions aren�t appropriate then compile time error is not caused but runtime error sets in.


But the following example of mine gives compiler error "incomparable types"

public class Test2{

Test2 a[] = new Test2[4];
Test2 b[][]= new Test2[2][];
public static void main(String a[]) {

Test2 t = new Test2();

if(t.a == t.b);
System.out.println("true");
}
}


Please clear my doubts.
 
Ranch Hand
Posts: 90
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rupasrivi Srivastava:
int k = 3;
...
Test(int a){ //one-arg constructor
super(a);
System.out.println("sub");}


Right, it's okay, there would be an error when calling super(k). Also if k is final int.


Test2 a[] = new Test2[4];
Test2 b[][]= new Test2[2][];

public static void main(String a[]) {

Test2 t = new Test2();

if(t.a == t.b);



t.a is of type Test2[], t.b is of type Test2[][]. I'm not sure why it does not compile. The same when trying to compare String[] and String[][]. However, Object[] and Object[][] do compare, giving false. Strange for me.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Czysciak:

t.a is of type Test2[], t.b is of type Test2[][]. I'm not sure why it does not compile. The same when trying to compare String[] and String[][]. However, Object[] and Object[][] do compare, giving false. Strange for me.



The same reason why line 1 will fail compilation:




A compile-time error occurs if it is impossible to convert the type of either operand to the type of the other by a casting conversion (�5.5).
 
Adam Czysciak
Ranch Hand
Posts: 90
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Czysciak:
[The same when trying to compare String[] and String[][]. However, Object[] and Object[][] do compare, giving false. Strange for me.



OK, I know now It's indeed a matter of types.
a) Object[][] t0 is-a Object[], is-a Object
b) String[][] t1 is-a Object[][], is-a Object[], is-a Object
c) String[][] t2 is-not-a String[], so it can't be compared

ad a)
for each i, t0[i] is Object[] - this is definition
so t0[i] is Object too (as every array is an Object)
so t0 is Object[]
so t0 is Object

ad b)
obvious, as String is-a Object

ad c)
why String[][] t2 is-not-a String[] t3?
then String t2[i] should be the same type as t3[i]
t2[i] is-a String[], is-a Object[], is-a Object
t3[i] is-a String, is-a Object
you can't compare String[] with String - incompatible types

I hope it explains the problem now.
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic