actual method that is called depends on the actual type of the object.
eg: 2 toString() methods, one of class Rectangle, other of class Square. varName.toString() calls rectangles toString() method
actual method that is called depends on the actual type of the object.
eg: 2 toString() methods, one of class Rectangle, other of class Square. varName.toString() calls rectangles toString() method
the compiler uses the declared type to check that the methods you are trying to use are available to an object of that type.
you cant access methods that are in the subclass if your object is declared as type superclass.
Superclasses don't have knowledge or access to subclasses!!
declared type of Object
It means the object is declared at the type of the cosmic superclass object (cosmic) superclass
inheritance-based polymorphism
IBP (inheritance based polymorphism) -> Common superclass, but methods are subclass specific.
polymorphism
literally means "many forms"
Shape reference variable can hold a Rectangle or Square object
You can only set references in a superclass => subclass or subclass1 => subclass1 way.
(down the hierarchy, (parent to child) or same hierarchic level (same child to same child) NEVER UP THE HEIRARCHY)
You can use super.method() to force the parent’s method to be called.
super
Just a way to refer to anything from the superclass.
Eg: you want to override a method from the superclass in the subclass but need the calculated value from the superclass. you access that method with a Super.methodName() inside the overriden methodName() in the subclass and do whatever to that value. Overrides Super keyword
unlike when we call a constructor with new, a call to super doesn’t create a new object.
Using super() allows you to call the constructor of the superclass to instantiate private fields of the subclass.
Used in the subclass's constructor
(@)override
public int SomeClass(int width, int heigh, int whateverElse){
return super( width, height, whateverElse) + this.width + this.height + whateverElse;
}
(psa ignore brackets at the at symbol)
I
hey i did that :D
superclass of any class that doesn’t explicitly extend some other class and the only class with no superclass and thus no super constructor that needs to be called.
refer back to this highlight (just previous highlight. I was rlly testing links lol)
the chain of super calls from each subclass to its superclass ends in the no-argument constructor of java.lang.Object
Ends the subclass to superclass no argument constructor calls using super() until it reaches the Cosmit Superclass or the Object Class
constructor doesn’t start with a call to super Java will automatically insert a call to super with no arguments.
Makes sure all instance variables are properly initialized before object use.
cannot access the private instance variables of the superclass directly.
side note: Fields (instance variables) can not be modified polymorphically, but can be inherited.
meaning, you can not override instance variables, but you can inherit them from the superclass.
overriding a method confused with overloading
overRIDE -> SAME method NAME AND SIGNATURE, different body. overLOAD -> SAME method NAME and DIFFERENT SIGNATURE, different body (parameters)