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

Show parent comments

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

12

u/bihnkim Oct 15 '16

So... four times?

4

u/[deleted] Oct 15 '16

4 times?

→ More replies (3)

3

u/anoamas321 Oct 15 '16

There are 10 types of people those who understand binary and those who don't

4

u/beb1312 Oct 15 '16

And those who understand ternary

→ More replies (1)
→ More replies (7)

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.

6

u/kaleyedoskope Oct 15 '16

Different tools for different jobs, id say. I had to write a recursive method to generate n characters of the Fibonacci sequence as part of an assignment, and it was unnecessarily complicated and bloated and stupid. But I also had a competition problem where I had to generate all n-length Collatz sequences and doing it recursively was way easier and more intuitive than not, because it was closer to how I was mentally thinking through the process. Same deal for DFS, pre/post order tree traversal, etc. Stuff that on its face "looks" recursive.

6

u/[deleted] Oct 15 '16

I had to generate all n-length Collatz sequences and doing it recursively was way easier and more intuitive than not

I think this is what the OP was getting at, recursion might be useful for personal projects since you're the only one reading the code and it's easy for you to understand what you did, but in the real world it is usually not worth it. I did similar things when I was still in school (pretty sure everyone had to write a recursive method for generating the Nth fibonacci number at some point in school haha), but now that I've been in the work force for a little over two years I almost never write recursive methods. Even if it can make concise, elegant code, it will be more work for your coworkers who eventually revisit the code you wrote to understand it. I usually just write more verbose functions (as long as they are not slower) where it's more explicit what is being done. Less work when someone looks at the code again (which could happen 5-10 years from when you wrote it for all you know) and is much, much easier to debug. There are no inherent advantages to recursion other than that it makes the code more concise and "elegant".

8

u/Xerodan Oct 15 '16

If you're only programming end-user programs, yes maybe. But many algorithms really rely on recursion. I couldn't live without it. Try to traverse a tree without recursion, it'd worse than getting waterboarded.

→ More replies (1)

3

u/[deleted] Oct 15 '16

exactly this. it's one of those tools that's great in interviews and looks really fancy, but in practice on a large development team it's too much trouble.

→ More replies (1)
→ More replies (2)
→ More replies (4)
→ More replies (9)
→ 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)