Hot answers tagged

14 votes

Parent class variable pointing to Child class

Just to add on to Bryan R's response, upcasting is typically automatic, as a side-effect of data structures or methods that store or utilize a parent type. There's not much point to the example in ...
Ben I.'s user avatar
  • 32.9k
7 votes

Parent class variable pointing to Child class

A common case is if there are multiple subtypes of Dog, you might create an ArrayList which can hold any type of dog. Or you might have a method that takes a reference to a Dog (doesn't care what ...
Bryan R's user avatar
  • 508
5 votes

Parent class variable pointing to Child class

This sort of thing happens all the time—is guaranteed to happen‚—if Dog is an interface or an abstract class. You've got this type, ...
Solomon Slow's user avatar
4 votes

Parent class variable pointing to Child class

I think we often confuse the idea of inheritance when we start thinking about it as one comes before the other. Really it's just a tool to reuse functionality and handle things in a uniform way. ...
Tom Bowen's user avatar
  • 427
4 votes

Parent class variable pointing to Child class

As written, you can understand the student's confusion: Dog rover = new Puppy( "Rover" ); You can "see" that you're creating an instance of ...
Phill  W.'s user avatar
  • 141
3 votes

Parent class variable pointing to Child class

This applies mostly to Java, though you can do something similar in Python. But my preference in this case is for Dog to be an interface, not a class. The interface defines the public protocol for ...
Buffy's user avatar
  • 36k
3 votes

Parent class variable pointing to Child class

This is the same as when we very commonly do Collection<Whatever> toys = new Arraylist<>(); Why do we declare toys ...
Edd's user avatar
  • 131
2 votes

Parent class variable pointing to Child class

A variable may contain different values at different times. For instance, we could have: ...
meriton's user avatar
  • 258
2 votes

Parent class variable pointing to Child class

The three best reasons I can think of as to why this principle is important in practice is the following: Collections Interfaces (Liskov substitution principle) Factory Functions As for how to teach ...
Abion47's user avatar
  • 121
2 votes
Accepted

When teaching Java, should I teach try-catch from the outset?

I'm not sure what the progression is in your education system, but the one I went through to learn programming as a complete novice in Java (~2015, in the USA, at a community college) went roughly ...
ggorlen's user avatar
  • 630
2 votes

When teaching Java, should I teach try-catch from the outset?

This answer is based on a software engineering point of view, in not teaching things that contradict best practices. (And there are lots of misconceptions and bad practices around exception handling, ...
Ralf Kleberhoff's user avatar
2 votes

When teaching Java, should I teach try-catch from the outset?

You may need to teach this concept, but I'd do it at the point it becomes be best option. If things can fail, some sort of test or similar action is needed. For a list of objects, one can return null ...
Buffy's user avatar
  • 36k
2 votes

Parent class variable pointing to Child class

When I was first learning Java, I encountered a variable of some concrete type being assigned to a variable of some interface type. I was a bit confused as to why someone would do that. A cohort ...
Fritz Sieker's user avatar
  • 1,133
2 votes
Accepted

Is there an array-like data type in Java that can cope with generic types better than the built-in array?

You probably want an ArrayList. They can be resized, however. Python has a similar thing. You should become familiar with the oracle docs of which the above link is a part. They are quite informative. ...
Buffy's user avatar
  • 36k
1 vote

When teaching Java, should I teach try-catch from the outset?

Create a type value or error (but nothing else: so not object) e.g. ...
ctrl-alt-delor's user avatar
1 vote

When teaching Java, should I teach try-catch from the outset?

In Java and many other languages that followed it, Exceptions are un-exceptional: e.g. it is not exceptional to get to the end of a file. Your example of removing an item from an empty list, is a ...
ctrl-alt-delor's user avatar
1 vote

Parent class variable pointing to Child class

I think, the answer is tangential to polymorphism and use of collections of objects others have mentioned. IMO, that doesn't get to the bottom of what's happening in real-world applications vs sample ...
Sergii Shymko's user avatar
1 vote

Parent class variable pointing to Child class

Ok, so I just read your edit about how the example of assigning a Puppy to a Dog collection is missing the point, because you ...
walen's user avatar
  • 111
1 vote

Best books for solid foundation build up for python and java

There is not best book. The answer to your question is "ANY". Just try any book for 1-2 chapters. Switch to another book, the a third. You will like 1 of the 3 more than the other two. ...
VISQL's user avatar
  • 136
1 vote

Explaining access levels and visibility in OOP

This year, I used the example of student grades that seemed to work pretty well (C++): The Student class has a private _grade. ...
MakisH's user avatar
  • 111
1 vote

Explaining how an Interface is a contract

If I were asked to describe how a pin-and-tumbler lock works - the kind of lock most of the developed world have on their front doors - I cannot imagine that the word "contract" would find ...
Steve's user avatar
  • 356

Only top scored, non community-wiki answers of a minimum length are eligible