I am trying to call the parent method in subclass. But I am getting stackoverflow error. The method name in both the class is same. If I change the method name it doesn't gives error. I am not understanding what is the reason behind it. Below is the code where I am calling display() method, in both the class which gives me stackoverflow error. If I change the name of method in any of one class it doesn't gives me error.
class inherit1 {
int i;
int j;
int k;
public inherit1() {
}
inherit1(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
void display() {
System.out.println("i=" + i + "j=" + j + "k=" + k);
}
}
class in1 extends inherit1 {
int l;
in1() {
}
public in1(int i, int j, int k, int l) {
super(i, j, k);
this.l = l;
}
void display() {
display();
System.out.println("l=" + l);
}
public static void main(String[] args) {
in1 i = new in1(12, 12, 12, 12);
i.display();
}
}
Change
void display() {
super.display();
System.out.println("k=" + k);
}
when you are calling display() it calls same child method againg and again causing stackoverflow.Instead you should use super keyword to reference parent method.
void display() {
display();
System.out.println("k=" + k);
}
With this method you keep calling the same method again, which in turn leads to a StackOverFlow error because you have created an infinite loop.
This will work better:
void display() {
super.display();
System.out.println("k=" + k);
}
Or even this would work:
void display() {
System.out.println(super.display() + ("k=" + k));
}
StackOverflow Error
Thrown when a stack overflow occurs because an application recurses
too deeply.
Problem is
void display() {
display(); // this line is culprit you are calling the same function again and again
System.out.println("k=" + k);
}
You should call parent's display method instead
Related
I am writing code in Java which has multiple methods and these methods have multiple variables. I want the other methods to access the variables of another method using actual and formal parameters. How can I do it?
I am pasting an example of the problem I'm facing.
Error : variable is not defined.
Code
public class example {
public void addition() {
int a = 0;
int b = 10;
int c = a + b;
}
public void result() {
System.out.println("The result for the above addition is" + c);
}
}
IM GETTING AN ERROR SAYING VARIABLE IS NOT DEFINED
You should declare c as global variable
public class Example {
int c;
public void addition() {
int a = 0;
int b = 10;
c = a + b;
}
public void result() {
System.out.println("The result for the above addition is " + c);
}
public static void main(String[] args) {
Example e = new Example();
e.addition();
e.result();
}
}
well, your java syntax is quite wrong... if you need to do an addition, you can do as follows:
public class Addition {
public static int addition(int a, int b)
{
int c= a + b;
return c;
}
public static void main(String[] args) {
int a = 1;
int b = 10;
int c = addition(a,b);
System.out.println("The result for the above addition is " + c);
}
}
where addition function does add a + b and return the result to your main method.
class A {
int a, b;
A(int i, int j) {
a = i;
b = j;
}
}
class B extends A {
int c, d;
B(int i, int j) {
c = i;
d = j;
}
}
public class HelloWorld {
public static void main(String[] args) {
A aa = new A(5, 6);
B bb = new B(3, 4);
System.out.println(aa.a + aa.b + bb.a + bb.b + bb.c + bb.d);
}
}
it gives error as
HelloWorld.java:9: error: constructor A in class A cannot be applied to given types;
{
^
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length
1 error
As you class B extends class A, you somewhere have to call the constructor of the class you are extending.
As your class A does not have a constructor with no arguments, you need to explicitly call the super constructor (the constructor from class A) as the first statement inside the constructor of your class B:
B(int i, int j) {
super(i, j);
// Your code
}
If you would have a no-args constructor in A, you would not need to do this, as the no-args constructor is implicitly called if no constructor call is specified:
B(int i, int j) {
// Your code
}
Is actually doing:
B() {
super();
// Your code
}
And as you do not have A() as a constructor, you get the error.
Check this one...
JVM always looking for no argument constructor.
class A {
int a, b;
A(int i, int j) {
a = i;
b = j;
}
public A() {
// TODO Auto-generated constructor stub
}
}
class B extends A {
int c, d;
B(int i, int j) {
c = i;
d = j;
}
}
public class pivot {
public static void main(String[] args) {
A aa = new A(5, 6);
B bb = new B(3, 4);
System.out.println(aa.a + aa.b + bb.a + bb.b + bb.c + bb.d);
}
}
Why the output is "021"? Why there are "0" and "1"(since "i" get "2" why it changes to "1")?
public class C {
protected int i;
public C(int i){
this(i,i);
System.out.print(this.i);
this.i=i;
}
public C(int i, int j) {
System.out.print(this.i);
this.i=i+j;
}
public C(){
this(1);
System.out.print(i);
}
public static void main(String[] args) {
C c=new C();
}}
C() calls C(1) which calls C(1,1)
C(1,1) prints 0 (the default value of this.i) and assigns 2 (i+j) to this.i
then C(1) prints 2 and assigns 1 to this.i
then C() prints 1
I think this is better for understanding:
public C(int i) {
this(i, i);
System.out.println("*"+this.i);
this.i = i;
}
public C(int i, int j) {
System.out.println("#"+this.i);
this.i = i + j;
}
public C() {
this(1);
System.out.println("#"+i);
}
Now, you can get the sequence of these methods when you invoke C();
Here the code commented, you will understand your problem now,
public class C {
protected int i;
public C(int i) {
this(i, i); // got to two parameter constructer and after the result print the next line
System.out.print(" + second "+this.i); // print value of i which is come from C(int i, int j) = 2
this.i = i; // set the value of i to 1
}
public C(int i, int j) {
System.out.print("first "+this.i); // print the value of i (in this case 0 the default value)
this.i = i + j; // set i to 2
}
public C() {
this(1); // got to one parameter constructer and after the result print the next line
System.out.print(" + Third is "+i); // print value of i which is come from C(int i) = 1
}
public static void main(String[] args) {
C c = new C();
}
}
I hope that help.
I have two methods:
public void test() {
for (int i = 0; i <= 10; i++) {
a();
}
}
public void a() {
// ...
}
Suppose the test method is fixed, I can not modify it.
How can I break the for loop in the a method?
You can throw an unchecked exception.
Another solution is to sub-class the class which has the first method and fix it so it can stop early e.g. if a() return true.
You can throw an exception, or you could simply use break;. This is a little less work, as you don't need to handle an exception at all.
Example:
public class Test
{
public static void main(String[] args)
{
foo();
}
public static void foo()
{
int i = 0;
for(i = 0; i <= 10; i++)
{
System.out.println("in loop: " + i);
if(i == 5) break;
}
System.out.println("end value: " + i);
System.out.flush();
}
}
The end value of i will be 5.
I have written the code but it displays Stackoverflowerror message.
class Sum
{
int ans=0,temp,temp2;
int getsum(int no)
{
if(no>0)
{
temp=no % 10;
ans=ans + temp;
getsum(no/10);
}
else
{
return ans;
}
}
}
class recsum
{
public static void main(String args[])
{
Sum s=new Sum();
int no,len;
len=args.length;
if(len==0)
{
System.out.println("No argruments are given ! ");
}
else
{
no=Integer.valueOf(args[0]).intValue();
System.out.println("Sum of digits= " + s.getsum(no));
}
}
}
You are over-complicating things a lot in your code. Here is a simpler working example:
public static int getSum(final String[] args, final int index) {
if (index < args.length) {
return Integer.valueOf(args[index]) + getSum(args, index + 1);
} else {
return 0;
}
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("You need to provide numbers as arguments.");
}
final int sum = getSum(args, 0);
System.out.println("Sum: " + sum);
}
You are supposed to be recursive, this is in the getSum function, because it is calling itself with differing parameters.
In recursive functions, you always need to have an exit branch that causes the calling to stop.
As sums won't change if you add 0 this can be exploited for a very clean exit.
The Stack overflow is normally because you never bottom out of the recursion.
Change class Sum to this:
class Sum {
int ans = 0, temp = 0;
int getsum(int no) {
if((no/10)-.5 >= 1)
ans += getsum(no/10);
else
return ans;
}
}
I'm not completely sure if this will work, and I can't compile it right now. I think this is one way to do it, but again, I'm not completely sure.
Program: Write a program to use Command Line Arguments.
class Sumnum1
{
int i,t,num,sum=0;
void getData(String s)
{
num=Integer.parseInt(s);
}
int digitSum()
{
for(i=num;i>=1;i=i/10)
{
t=i%10;
sum=sum+t;
}
return sum;
}
public static void main(String arg[])
{
int ds=0;
Sumnum1 obj=new Sumnum1();
obj.getData(arg[0]);
ds=obj.digitSum();
System.out.println("sum of digit="+ds);
}
}
BY :ANKIT AGRAWAL (A.A.)