• 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

doubt

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all

The problem is with the line (1)



class Parent
{
void method1()
{
System.out.println("Parent's method1()");
}

public void method2()
{
System.out.println("Parent's method2()");
method1();
}

}

class Child extends Parent
{

Child()
{
//method2
}

public void method1()
{
System.out.println("Child's method1()");
}

public void method2()
{
System.out.println("child's method2()");
method1();
}

public static void main(String args[])
{
//Parent p=new Child();// (1) when i invoke the line (2) with this line uncommented it throws error saying can't resolve symbol why it so.since the instance type is child why it throws the error
Child p = new Child();
p.method2();
int[] a={1,2,3,4,5};

p.arr(a);
for(int i=0;i<a.length;i++)
System.out.println(a[i]);

}
void arr(int b[])
{

for(int i=0;i<b.length;i++)
System.out.println(b[i]);
}
}
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would suggest you read up on how inheritance works.
whether methods of parent are accessible to child or is it the other way around.
well as you can see in your code, the arr() is a method of the child and not of the parent. whereas the method1() method is an overridden method of parent's method1().

Look into some stuff on inheritance and i think it should be clearer then.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi paramesh,

Whenever u call a method thro' derived class object with base class reference, that method should be present in the base class as well as derived class.

Parent p=new Child();
p.arr(a);

In this program u dont have the method(arr) in the base class, though it is present in the derived class. It method(arr) must be defined in base(Parent calss)to clear the error.

hope u understand....

regards.......
sakthi
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic