File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt" Watch "doubt" New topic
Author

doubt

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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

Joined: Mar 20, 2005
Posts: 256
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.


Life called,so here I am.<br />Cheers<br />Niki.:-)
Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
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


" Don't be afraid of pressure. Remember that pressure is what turns a lump of coal into a diamond... " <br /> <br />Thanks & Regards...<br />Sakthi<br />SCJP1.4, OCA
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt