In: Computer Science
Firstly, there are some common syntax errors and spelling mistakes in the given program. We need to resolve them first.
Then, we tried to compile the program we got two errors as
This happens because members/attributes of a class which are declared by using private access specifier are not accessible outside that class.
Here, 'u' and 'v' data members of a class 'AClass' are declared with private access specifier and they are being accessed in 'BClass' class to perform operation. Hence, we are getting this error.
To solve this error, data members 'u' and 'v' are to be declared by using protected access specifier. Because members/attributes declared by using protected access specifier are only accessible within that class and derived class only.
Below is the code which solves the problems and errors we came up earlier
public class AClass
{
protected int u ;
protected int v ;
public void print(){
}
public void set ( int x , int y )
{
}
public AClass()
{
}
public AClass ( int x , int y )
{
}
}
class BClass extends AClass
{
private int w ;
public void print()
{
System.out.println (" u + v + w = " + ( u + v + w ) );
}
public BClass()
{
super() ;
w = 0 ;
}
public BClass ( int x , int y , int z )
{
super( x , y ) ;
w = z ;
}
}
Following is the Driver class which shows the output of operation and working perform in above classes
public class Main
{
public static void main(String[] args) {
BClass b=new BClass(10,20,30); //object of BClass
b.print();
}
}
OUTPUT
I hope that I have solved your query. If any problem occurs please mention it in comment section.
Thank you.