• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt

 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • 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]);
}
}
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • 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.
 
Sakthi Kani
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic