Hi,
I am writing a program that has 3 files:
File 1: One.java
File 2: abstract1.java
File 3: child.java

Child is a subclass of abstract1 and abstract1 is an abstract class.

I am trying to call a method in the Child class from One.java .
Currently i have this:

One.java

public class One
 {
  public static void main (String[] args)
   {
    
    int side=5;
     abstract1 cir = new child();
     child.child(side);
	   }
   }

abstract1.java

public abstract class abstract1
{
	public abstract void computeArea();
	
}

child.java

public class child extends abstract1
{
	int side;
	public child(int side1)
	{
		side=side1;
		System.out.println("Hi"+side);
	}
	public void computeArea()
	{
		int area;
		area = side * side;
	}
}

The problem is that when i compile, it get the following errors:

One.java:
For the line, abstract1 cir = new child(); :
Cannot find symbol
symbol: constructor child()
location: class child

For the line ,child.child(side); :
Cannot find symbol
symbol: method child (int)
location: class child

So im not sure how to properly instantiate the Child class in the first place, and then how to call a method after.

Thanks

Because "abstract1" does not declare that method, it cannot be called on an instance that is referenced as an abstract1 instance. Either you have to cast or declare it as "child" or you have to declare an abstract "child(side)" method in abstract1.

For the line, abstract1 cir = new child(); :
Cannot find symbol
symbol: constructor child()
location: class child

Here, you are creating a new child instance alright, but there is no such constructor for the child class.

For the line ,child.child(side); :
Cannot find symbol
symbol: method child (int)
location: class child

Here again, you are caught calling a method in the child class that does not exists. Yes you do have a method public child(int side) but let me tell you that since this method shares the name of the class it becomes a constructor. Also the way you are calling it, with directly the name of the class as a reference, it needs to be static.

Also keep in mind the casting masijade tells you about.

I have the feeling what you actually meant to do, was this:

public class One {
    public static void main (String[] args) {
        int side=5;
        abstract1 cir = new child(side);
        child.computeArea();
    }
}

However, since computeArea is void, and you do not "print" anything from that method, you will get no visible clue (except that it produced no error) that it successfully ran.

But for that too computeArea() should be static.
OR
It should be called with the object name (cir) casted to child.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

But for that too computeArea() should be static.
OR
It should be called with the object name (cir) casted to child.

No, computeArea is declared in abstract1, so it perfectly legal to be called on an instance referenced as an abstract1 instance. And if the actual instance has overriden that method, then the overridden version will be used. This is how it is suppossed to work.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Ach, I meant to change that. It should, obviously, be

cir.computeArea();

not

child.computeArea();

but it doesn't need to casted. ;-)

This question has already been answered. Start a new discussion instead.