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 ...
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 ...
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, ...
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.
...
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 ...
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 ...
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 ...
2
votes
Parent class variable pointing to Child class
A variable may contain different values at different times. For instance, we could have:
...
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 ...
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 ...
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, ...
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 ...
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 ...
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.
...
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.
...
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 ...
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 ...
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 ...
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. ...
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.
...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
java × 60self-learning × 9
programming × 9
object-oriented-paradigm × 9
ap-computer-science-a × 9
secondary-education × 8
resource-request × 5
teaching-analogy × 5
curriculum-design × 4
undergraduate × 4
textbook × 4
algorithms × 3
project × 3
exercises × 3
data-structure × 3
language-choice × 3
c × 3
lesson-ideas × 2
introductory-lesson × 2
grading × 2
python × 2
labs × 2
c++ × 2
resource-information × 2
looping × 2