Obscure Ami Obscure Ami - 2 months ago 6
Java Question

This is very simple java code, but can't identify the error. I an getting syntax error

This is my simple java code. When I compile/run the program, Eclipse IDE shows a syntax error. The syntax error does not make any sense to me

class A {
int x;
int z;
int s;

A(int a,int b) {
x=a;
z=b;
}

void display() {
System.out.println("x+y :"+(x+z));
}
}

class B extends A
{
B(int a, int b, int c) {
x=a;
z=b;
s=c;
}

void display() {
System.out.print("In B class...");
System.out.println("x+y+s :"+(x+z+s));
}
}

public class Simple {
public static void main(String[] args) {
A ob=new A(10, 20);
B ob2=new B(20, 30, 40);
ob.display();
ob2.display();
}
}

Answer

First off, posting the error given by eclipse would be helpful.

But in any case, your syntax errors are stemming from the fact that you aren't defining the datatype of your variables, such as x, z, s, etc.

For example,

int x = a;
int z = b;

etc.

Comments