?
Solved

runtime polymorphism

Posted on 2004-11-20
9
Medium Priority
?
220 Views
Last Modified: 2010-03-31
public class child extends base
{
 int i=20;
 void m1()
 {
  System.out.println("child"+i);
 }
 public static void main(String args[])
 {
  base a =new base();
  base b=new child();
  a.m1();
  b.m1();
  System.out.println("i1:"+a.i);
  System.out.println("i2:"+b.i);
 }
}


i am getting the O/P
Base=10
Child=20
i1:10
i2:10

i thought output will be
Base=10
Child=20
i1:10
i2:20

theroitically
when we define like this
 base b=new child();

at runtime JVM will take object type not reference (here Base class)  the o/p will be i2=20 why i2 is printed 10;
0
Comment
Question by:chaitu chaitu
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:baboo_
ID: 12637110
In your child class, change this:

int i = 20;

to:

child()
{
     i = 20;
}

and your problem will be solved.

baboo_
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12637123
ohno

i forgot to paste the base class

package example;

class Base
{
 int i=10;
 void m1()
 {
  System.out.println("Base="+i);
 }
}
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12637140
Chaituu,
in ur class child remove the line:

>>  int i=20;

and initialize the i in the constructor as baboo said
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Accepted Solution

by:
baboo_ earned 500 total points
ID: 12637170
Here's why:

Think of your child object as an onion.  On the outside layer (the "child" object layer), it has an integer variable, i, and the method, m1.  Inside that layer, the "base" class also has an integer variable, i, and a method m1.  Inside THAT layer is the ultimate base class, the Object class, which also has all of its own instance variables and methods.

Now, since you overrode the base class's m1 method with the child method m1, the JVM knows, regardless of the reference type, which method to execute - it always uses the lowest version on the inheritance tree.

But since BOTH the child AND base objects have an integer variable named i, and the reference is of type base, it's going to use base.i instead of child.i

You shouldn't declare i again in child.  It already exists in the "base" class layer, which exists within the child object layer whenever you instantiate a child object.  All you need to to is assign a value to it.  The best way to do that is to use a constructor (which is what I suggested in my last comment).

A constructor is a method you call when you use the 'new' operator.  Notice that when you create a child object, it kind of looks like you're calling a method:

base b = new child();
                       ^
                        |
See?  Looks like a method.  That's because it *is* a method - it's a constructor.  Java will provide these for you automatically if you don't write them, but you sometimes need to write them (like in your case).

Here's how you'd do it for your child class:


class child extends base
{

             child()
             {
                      // initialization stuff
                      // goes in here, like:

                       i = 20;
              }

              // the rest of the code you wrote for the child
              // class is here....
              // ...
              // ...

}

This way, there's only one integer variable named i - it exists in the base class, but your child class inherits it.  So you don't need to declare it - you can just use it as thought it was already declared (since it was, in the base class).

Anyways, I hope this helps.  Please, don't hesitate to ask me more if I haven't been completely clear about all this.

baboo_


0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12637356
public class Child extends Base
{
int i;
Child()
{
  i =20;
}
 void m1()
 {
  System.out.println("Child="+i);
 }
 public static void main(String args[])
 {
  Base a =new Base();
  Base b=new Child();
  a.m1();
  b.m1();
  System.out.println("i1:"+a.i);
  System.out.println("i2:"+b.i);
 }
}

i changed like this still getting the same o/p

Base=10

Child=20

i1:10

i2:10
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12637370
chatuu,

u didn't do what i told u i mean the following:

1- in the base class make the member field (i) protected:
protected int i;
2- in the base class intialize (i) inside the constructor
public Base()
{
i = 10;
}
3- in the child class remove the member field (i);
>> int i; // <---- remove this line
4- in the child class make the construtor as in ur last comment:

Child()
{
  i =20;
}
0
 
LVL 3

Expert Comment

by:vikraman_b
ID: 12641449
Hi chatuu...
what exactly u want..?
As i told u earlier
Base a = new Base();
Base b = new Child();
a.m1();//calls base  bcos reference is base object
b.m1();//calls child bcos refernence is child object

but for member variables
a.i;//calls base class member variable
b.i;//calls base class member variable although its a child object refernce..

what the thing u want to do..?
do u want to print b.i = 20.


0
 
LVL 3

Expert Comment

by:vikraman_b
ID: 12641476
what petmagdy
 told is just  to get the output
since protected member can be accessed from subclass
==========
Base=10
Child=20
i1:10
i2:20
----------------

Then in what is the meaning of runtime polymorphism??
0
 
LVL 3

Expert Comment

by:baboo_
ID: 12652026
You don't need to declare the variable i in class Child because the Child class inherits the variable i from its parent class.  In fact, just declare the variable i in the Base class, and initialize it in the constructor for each class.  Here's what you need:

=== Base Class ==================

class Base
{
     int i;

      Base()
      {
           i=10;
       }

      void m1()
      {
           System.out.println("Base="+i);
      }
}

=== End Base Class ===============
=== Child Class ==================

public class Child extends Base
{

      Child()
      {
            i =20;
      }

      void m1()
      {
            System.out.println("Child="+i);
      }

      public static void main(String args[])
      {
            Base a =new Base();
            Base b=new Child();
            a.m1();
            b.m1();
            System.out.println("i1:"+a.i);
            System.out.println("i2:"+b.i);
       }
}

=== End Child Class =================
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question