The Artima Developer Community
Sponsored Link

Java Answers Forum
Why such behaviour?

3 replies on 1 page. Most recent reply: Mar 2, 2006 7:51 PM by Sanjay Perera

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Sanjay Perera

Posts: 42
Nickname: angel6
Registered: Apr, 2004

Why such behaviour? Posted: Mar 1, 2006 7:52 PM
Reply to this message Reply
Advertisement
Parent.java
-------------
class Parent {
int i = 111;
Parent() {
this.returnVal();
System.out.println("SUPER:NO ARG");
}

public void returnVal(){
System.out.println(i);
System.out.println("Super method");
}
}

Child.java
------------
class Child extends Parent {
int i=55;
Child(){
this.returnVal();
System.out.println("Child Empty construct");
}

public void returnVal(){
System.out.println(i);
System.out.println("Child method");
}
public static void main (String args[]){
Child df = new Child();
}
}

Question A
-------------
When running the Child program the out put will be...

0
Child method
SUPER:NO ARG
55
Child method
Childt Empty construct

Why the child method was called even when the Parent's contructor was calling the Parent's returnVal() method? Please explain the concept and the reason.
Question B
-------------
If it called child’s returnVal() then why it didn’t print 55 at first place (Child’s i). From where it got 0? Even the Parent class’s i is initialized to 111. Please explain the concept and the reason.

Question C
------------
How to make the method returnVal() of Parent class to be called from the Parent’s constructor using above two programs. Without making the Parent’s returnVal method private.

Question D
-------------
Does anyone know any online tutorial which teaches this behaviour?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Why such behaviour? Posted: Mar 2, 2006 2:36 AM
Reply to this message Reply
repost your code using the java tags as shown on the right of the input field.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Why such behaviour? Posted: Mar 2, 2006 6:29 AM
Reply to this message Reply
Ok, here is what happens.

Call of Child constructor
1. Child's class variables are declared
2. Child constructor calls the constructor of it's parent class
2.1 Parent's class variables are declared
2.2 Parent's constructor calls the constructor of it's parent class, which is Object
2.2.1 //I don't care what happens in Object
2.3 Parent's class variables are initialized
2.4 Parent's constructor is executed
2.4.1 Child's returnVal overrides parent's method, so it is called instead of Parent's returnVal. The class variables in Child are not yet inizialized, just declared, so the value of i is default = 0
3. Child's class variables are initialized
4. Child's returnVal is called, returns 55

Sanjay Perera

Posts: 42
Nickname: angel6
Registered: Apr, 2004

Re: Why such behaviour? Posted: Mar 2, 2006 7:51 PM
Reply to this message Reply
Thanks, sorry for the not formmating it!!

Flat View: This topic has 3 replies on 1 page
Topic: Class URL Previous Topic   Next Topic Topic: calling methods from main

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use