ABOUT ME: I am learning Java because I want to become a software developer someday and I came across the topic of subclasses and their ability to inherit from superclasses on tutorialspoint.com.
where I may need correction
- So let's say I made a class with an instance variable declared as int age; It also has a method for printing its age.
class Superclass {
int age = 3;
public void printAge() {
System.out.println(age);
}
}
- Then I make a subclass that extends to the initial class. The subclass would inherit the instance variable, correct?
class Subclass extends Superclass {
//invisible instance variable int age = 3;
}
- So, if I used the printAge() method on a subclass object, it should print out a 3, right?
Subclass obj = new Subclass();
obj.printAge(); --> 3
- So far, using the (.printAge) method that was inherited, I could access the super instance variable (age) without the super keyword. But, if I were to set up an instance variable for the subclass from the beginning, I would have to use the super keyword to get the same output as before, right?
class Subclass extends Superclass {
//invisible instance variable int age = 3;
int age;
}
Subclass obj = new Subclass();
obj.printAge(); --> null
- Let me override the print function for the subclass to make what I mean a little more clear.
class Subclass extends Superclass {
//invisible instance variable int age = 3;
int age;
public void printAge() {
System.out.println(super.age); -- > 3
System.out.println(age); -- > null
}
}
- I am pretty sure that before I made an instance variable explicitly for the subclass, I could reference the super inherited age variable using (age). But only after I made the instance variable explicitly would I have to use the super keyword to differentiate the super and sub instance variables. Does this mean that the inherited super variables will take the spot of an instance variable until the programmer explicitly assigned one for the subclass, in which case only THEN would you have to use the super keyword? Am I correct in this assumption?
I tried to find out why is it that I didn't need to use the super keyword to access an inherited instance variable until I explicitly made one in the subclass.
Assuming I am correct, I wasn't aware that a subclass would LITERALLY inherit a super variable, and would also allow you to make an instance variable of the same name in that super class, meaning that you can utilize two variables at all times. Maybe I am dumb, but it didn't click in my head until now, assuming I am correct in thinking this.
EXTRA: I would appreciate any advice on the steps to becoming a great software developer. For example, "Where should I go from here after learning Java", or "How can I apply the Java code to make software?" to be more specific. Some advice for internships or preparations for internships would be welcome as well.