Basic Flashcards
T or F
In inheritance, private methods are final.
True.
All compilers will treat private methods as final. The compiler will not allow any private method to be overridden. Likewise, all compilers will prevent subclasses from overriding final methods.
Protected members are accessible within a package and inherited classes outside the package.
T or F
True
T or F
We cannot override private metods
True
What’s another word for an object?
Instance
What’s another word for an instance?
Object
House blueHouse = new House("blue") House anotherHouse = blueHouse;
System.out.println(blueHouse.getColor);
System.out.println(anotherHouse.getColor);
What color will print? and why?
Blue and blue
Because there are two references to the same objects.
https: //www.udemy.com/course/java-the-complete-java-developer-course/learn/lecture/9331916#notes
2: 29
T or F
The call super() must be the first statement in each constructor.
True
Can a constructor have both, a call to super() or this()?
False. Can’t have both
Can a constructor have both, a call to super() or this()?
False. Can’t have both
T o F
Can I use the “this” keyword in a getter?
True. In the getters there are no parameters, so the “this” keyword is optional.
What is the super keyword in Java?
Super keyword in java is a reference variable which is referred immediate parent or super class object. In other words, Super keyword is used to access the members of the parent class.
Super keyword point to immediate parent class object. It works with objects only of the parent class.
Write some points about super keyword in java?
There are some use of super keyword in java. Super keyword can be used to refer parent class instance variables. Super keyword can be used to invoke parent class method. Super keyword can be used to invoke parent class constructor.
Can we access parent class variables in child class by using super keyword?
Yes.
class Parent { int a = 20; }
class Child extends Parent { int a = 30;
void show() { System.out.println(a);//print child class value of a System.out.println(super.a);//print parent class value of a }
public static void main(String args[]) { Child c = new Child(); c.show(); } }
Output:30
20
Can we use both “this” and “super” in constructor?
No, because both this and super should be the first statement.
What is the difference between this() and super()?
There are some differences between java this() constructor and super() constructor.
Java this() It is used to access the current class constructor. It can be used inside another constructor of the same class. It can be used to remove ambiguity errors when we have data members and local are the same name.
Can we use super keyword in the static method of a subclass for calling the parent class method?
No, We cannot use super keyword in static methods because it belongs to the immediate parent object and static belongs to the class level.
Difference Between this() and super() ?
Super keyword is always pointing to base class features and this keyword is always pointing to current class features.
T or F
Overloading does not have anything to do with polymorphism.
True.
But Java developers often refer to overloading as Compile Time Polymorphism.
What is the difference between Overloading andOverriding?
- Overloading is about same function have different signatures.
- Overriding is about same function, same signature but different classes connected through inheritance.
Is this overriding or overloading?
By extending the parent class, the child class gets all the methods defined in the parent class.
Overriding
____ means defining a method in a child class that already exists in the parent class with the same signature (same name, same arguments).
Overriding
Is this overriding or overloading?
The compiler decides which method is going to be called based on the method name, return type and argument list.
Overloading