r/learndota2 • • Oct 14 '16

All Time Top Post [Java] How does inheritance really work?

I have a following class:

public class Parent {
    private int number;

   // more stuff
}

And another, which inherits from Parent:

public class Child extends Parent {
    public void setNumber(int newNum){
        this.number = newNum;
    }
}

I always thought Child was a copy of Parent, but you could add stuff to it (and possibly change something). So I would expect it already has the 'number' attribute. However this will never compile as there isn't anything named like that. Why?

EDIT: I am sorry, guys. I thought this was /r/learnprogramming. I don't play dota and I am not even subscribed so this is a mystery to me.

2.8k Upvotes

245 comments sorted by

View all comments

3.3k

u/fuxorfly Oct 14 '16

If its private, you can't access it from derived classes; change the variable to be 'protected', and you can modify the variable from derived classes.

EDIT - also, this is the dota subreddit, you might be better off in the java sub ;)

1.8k

u/SlowerPhoton Oct 14 '16

OMG, you are right! I don't even play dota! How the fuck this happened?!

3.5k

u/ProfessorMonocle Oct 15 '16

public class java extends learndota2

366

u/Bosticles Oct 15 '16 edited Jul 02 '23

plucky deer rob future complete cover bedroom sable snow price -- mass edited with redact.dev

329

u/Noclue55 Oct 15 '16

As someone who doesn't get the joke, but understanding that you are a very knowledgeable person I have this to say.

96

u/ExistentialEnso Oct 15 '16

The reality is it barely shows any knowledge at all. This is third week of CS101-level knowledge. It's about as basic as it gets with coding jokes.

83

u/mylivingeulogy Oct 15 '16

My csc101 class barely touched loops 3 weeks in.

0

u/[deleted] Oct 15 '16

[deleted]

21

u/mylivingeulogy Oct 15 '16

Hahaha. We did numbering systems for two weeks first.

45

u/voltzroad Oct 15 '16

Every cs class teaches numbering systems for the first 2 weeks. I've learned binary literally 100 times

→ More replies (0)

7

u/GroggyOtter Oct 15 '16

That doesn't sound like Java. That sounds like intro to programming / programming logic and design.

→ More replies (2)

4

u/regenzeus Oct 16 '16

There are very different types of programming classes. In some you get to do useful stuff pretty quickly. In others you go deeper and will progress much much slower. When I studied this stuff we only programmed in C for the first 5 semesters. But we learned what exactly the code will do. You know how an ALU works and all that stuff.

→ More replies (1)

9

u/rainer51 Oct 15 '16

My cs101 class barely got out of PowerPoint

→ More replies (5)

7

u/InkpenLoL Oct 15 '16

I'm in CS101 myself -- oddly enough I learned this on my 3rd day? Maybe 4th.

week 5 right now, on week 3 we were learning how to create nested loops, conditional statements, and a couple more things I could barely understand :D

13

u/jesbu1 Oct 15 '16

Lol week 3 for me we already started talking about tree recursion and I was like wtf.

0

u/[deleted] Oct 15 '16

[deleted]

4

u/jesbu1 Oct 15 '16

Yeah I totally understand recursion now, we're doing much more complicated recursion but it was just a total mindfuck at first.

12

u/[deleted] Oct 15 '16

After years of programming I've come to this conclusion about recursion: don't.

It's a pattern that's confusing to read and confusing to write, and always has tons of bugs.

→ More replies (0)
→ More replies (1)

20

u/Hedoin Oct 15 '16

Most jokes like this in even semi-specialised fields rub me the wrong way. They are a stretch at best and call on very basic knowledge, but people who understand it realise others may not and therefore accept it as an in-joke.

11

u/ProfTrippinBalls Oct 15 '16

You just explained my brain better than my brain does.

→ More replies (1)

3

u/GroggyOtter Oct 15 '16

It's about as basic QBasic as it gets with coding jokes.

FTFY ^_^

→ More replies (1)
→ More replies (38)
→ More replies (10)
→ More replies (6)