• 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

question on instance variable...

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once again I am confused...
I think Instance variables invocation depends upon the reference type...
where as method calling depends upon the object type...

Then the anser will be Geography....But actual answer is Mathematics...
(MainTest mt = new Test();
mt.takeTest1();
here it prints Mathematics...it is fine...)


class MainTest {

protected String subject1="Mathematics";



protected void takeTest1(){

System.out.println(subject1);

}

}


public class Test extends MainTest{

protected String subject1="Geography";



public static void main(String... args) {

Test test1 = new Test();



test1.takeTest1();

}

}



Your Answer
Compilation Error

Runtime Exception

Mathematics

Geography
 
Siva Sekhar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here i forgot to mention the source...
question 12
http://www.geekevaluation.com/Exam/question
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Siva

The behaviour is correct.

Bottom line is:
The subclass(Test, in your example) inherits the public,protected, default(only if the subclass is in the same package) variables and methods from the superclass(MainTest, in your example).

If the subclass wants a special behaviour then it must override the method that it has been inherited from the superclass. In your example, you must have inherited the method takeTest1() in such a way it prints the subject as 'Geography' on to the console.

Since the method takeTest1() has not been overrided in the subclass Test, a call to the method takeTest1() on the object type(of the subclass) Test will eventually call the method takeTest1() of the superclass MainTest(due to Inheritance).

If you want that it will print 'Geography' instead of 'Mathematics' then you must include (by overriding) the method takeTest1() in the subclass Test as follows:

public class Test extends MainTest {
//if you remove this line it will print
// still 'Mathematics'(inherited variable value)
protected String subject1 = "Geography";

protected void takeTest1() {
System.out.println(subject1);
}

public static void main(String... args) {
Test test1 = new Test();
test1.takeTest1();
}
 
Krishnamoorthy Vuyala Muralidharan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Siva

The behaviour is correct.

Bottom line is:
The subclass(Test, in your example) inherits the public,protected, default(only if the subclass is in the same package) variables and methods from the superclass(MainTest, in your example).

If the subclass wants a special behaviour then it must override the method that it has been inherited from the superclass. In your example, you must have inherited the method takeTest1() in such a way it prints the subject as 'Geography' on to the console.

Since the method takeTest1() has not been overrided in the subclass Test, a call to the method takeTest1() on the object type(of the subclass) Test will eventually call the method takeTest1() of the superclass MainTest(due to Inheritance).

______________________________________________________
If you want that it will print 'Geography' instead of 'Mathematics' then you must include (by overriding) the method takeTest1() in the subclass Test as follows:

public class Test extends MainTest {
//if you remove this line it will print
// still 'Mathematics'(inherited variable value)
protected String subject1 = "Geography";

protected void takeTest1() {
System.out.println(subject1);
}

public static void main(String... args) {
Test test1 = new Test();
test1.takeTest1();
}
}
______________________________________________________

Hope this helps.

Kind Regards
Kris
 
Siva Sekhar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry...

Now I got the point...
Because of inheritance..it is calling the Taketest1() method...
In that it is printing the super class variacle...
am i correct...?
 
Siva Sekhar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Kris...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic