r/javahelp • • Apr 20 '20

Inheritance question

This is the code the professor gave us and I have no idea what to do with it. He wants me to print out i and j. I've tired changing a few things but I don't understand the code in general and I am always getting an error in class inherited. I tried changing "class B extends A" to "class B extends Inheritance" and this gets rid of that error but at the "System.out.println(obj.i + " " + obj.j);" there's always an error with i&i that states they cant find them. I named the super class Inheritance and the subclass inherited btw. Thank you ahead of time for any help.

class A

{

public int i;

public int j;

A()

{

i = 1;

j = 2;

}

}

class B extends A

{

int a;

B()

{

super();

}

}

class super_use

{

public static void main(String args[])

{

B obj = new B();

System.out.println(obj.i + " " + obj.j);

}

}

0 Upvotes

8 comments sorted by

3

u/Camel-Kid 18 year old gamer Apr 20 '20

What exactly is the problem, what did the professor want you to do in the first place..

-2

u/lightprojector Apr 20 '20

Oh sorry about that. The question was to printout i and j.

5

u/GuyWithLag Apr 20 '20

First of all, you're asking us to donate some of our time, but haven't donated your time; please be considerate:

  • format the code (yes, that is possible even with reddit's weird editor.

  • clearly state a) the problem, b) what you want to achieve c) what you have tried so far.

1

u/lightprojector Apr 20 '20

Im sorry for all of that, I never formated code on reddit but I will learn it as soon as I can. I literally copied an pasted the question. This was part a of the question and all it asked was to get the output which I assume is i and j. I was a little stressed about this question so I kinda gave up. Ill give it another shot tonight and see if I can work it out. Thank you just for commenting.

2

u/GuyWithLag Apr 20 '20

I was a little stressed about this question so I kinda gave up

Why get stressed? If you don't understand it, that's OK - and that will continute to happen as long as you work in the field (I'm here since the late 90's, and still get that); the trick is making sure you use it to your advantage.

1

u/lightprojector Apr 20 '20

It's really the feeling of being rushed to learn something to the point of actually understanding it. I know it takes time and failure but I dont always have time to do so. When it does happen I tend to get frustrated/stressed sometimes, but I it's part of the process.

1

u/FabulousFell Apr 20 '20

super_use as a class name does not follow java conventions.

1

u/lightprojector Apr 20 '20

Looking back at it with a clear mind I think I have an idea what to do but I will get back to it tonight.