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.

3

u/Antonin__Dvorak Oct 15 '16

What kind of CS program starts with Java?! That's like an intro mechanics course starting out with Ferrari engines.

14

u/ExistentialEnso Oct 15 '16

It's extremely common these days. Most CS programs start people off with either Java or Python.

I also disagree with that analogy. I guess you mean in terms of complexity, which I can kind of see, but if anything, Java is more like a minivan. It's inefficient and bulky, but it has a lot of utilitarian value and is easy to use, so a lot of people fall back on it anyway.

3

u/Antonin__Dvorak Oct 15 '16

I see what you mean. The analogy was meant to demonstrate how complex Java is - it's really easy for a beginner to get lost in the nitty gritty of the language constructs, and miss out on important CS concepts as a result.

Python makes more sense, but ideally it would be something even simpler - a functional language like Scheme, for instance.

6

u/ExistentialEnso Oct 15 '16

Gotcha, I can see how the analogy makes sense from that angle, you're just going to always get a lot of funny looks if you compare Java to a sports car.

Starting with functional programming is one of those things that makes a lot of sense from an academic perspective but almost no sense from a practical one. If the goal is to prepare someone as much as possible for the software industry without requiring further degrees, it's just a waste of time. If someone is going to go on to get an MS or PhD, sure.

But the industry is a lot different than academia. For instance, the vast majority of coders are never going to have to manually write a sort function or calculate the Big-O notation of something outside of a job interview. Most of the best coders I've worked with were not CS majors, actually, many having no academic programming experience at all.

The single biggest factor actually under someone's control (i.e. not just raw talent) that will determine how good of a coder someone will be is simply how much hands on experience they have. You just need to code a lot of things in a lot of different languages and frameworks, so you have the flexibility to adapt to new technologies. This is one of the most rapidly changing industries, and you have to be ready to keep up.

→ More replies (0)

3

u/JagerNinja Oct 15 '16

What do you suggest they start with? Most CS programs start with Java or Python.

5

u/Antonin__Dvorak Oct 15 '16

Schools like MIT, Caltech and Waterloo start with functional languages like Scheme. This way you can learn core computer science concepts (recursion, lists, trees, sorting, algorithms, structures, etc etc) and good practices / good documentation without getting bogged down in the nitty-gritty language constructs of commercial languages.

5

u/Severian71 Oct 15 '16

This certainly used to be true of MIT, and I still think that you could do a lot worse if you're learning CS than to carefully go through SICP (freely available here: https://mitpress.mit.edu/sicp/) which used to be their intro text and uses a small subset of Scheme, which is already a pretty small language. If you understand everything in SICP well you will know at least some important things that in my experience a lot of people with degrees in CS don't.

That said, didn't MIT switch to using Python for their intro class a while back? That was my understanding at least.

→ More replies (0)

4

u/mrpaluza Oct 15 '16

Ah, no. We started with Python.

Source: at one of those three colleges 😉

→ More replies (0)

3

u/pretendsnothere Oct 16 '16

Caltech's intro class is in Python, and has been for a while

→ More replies (0)

1

u/mabbass Oct 15 '16

My program started with visual basic. Java is next.