Java_Strings_Encapsulation_Inheritance Flashcards
Difference between creating a java string with same values using a “new” keyword and a string literal as below:
String myStr1 = new String(“Mulligan”);
String myStr2 = new String(“Mulligan”);
AND
String myStr1 = “Seagan”;
String myStr2 = “Seagan”;
Which java package has the String class?
java.lang.String
Explain sub String operations with examples.
As strings are immutable, explain a situation where they can cause memory leaks?
In the example below, as a is now pointing to a new string, the unreferenced heap are will cause a memory leak.
What are StringBuilder and StringBuffer in Java?
Difference between pass by value and pass by the object?
- Pass by value will not reflect the value changed inside the called method() to the caller, as it creates a new stack for the called method with a local primitive value copied(only) and pops off the stack when returned to the caller.
- Whereas pass by the object will reflect the change to the caller because the local reference created inside the called method will reference the same object on the heap.
What is encapsulation in Java?
What is the default access modifier in Java?
When an access modifier is not specified in a class, the default access is available within the declaring class and the classes in the same package.
What are the different access modifiers in Java?
What is inheritance in Java?
How many classes can be inherited by a class in Java?
Each class in java can only extend one class.
Can we inherit the constructor of the parent class?
No. Every class should have its own constructor. It cannot inherit a constructor from the parent.
What is a chaining constructor?
- When we do not have access to parent class constructor, we mostly try to use the getters and setters of the parent class by making them public or protected. But that breaks the idea of proper encapsulation.
- Chaining constructor allows accessing parent class constructor using super keyword. And, this super() should be the first line in the child’s constructor.
What happens when we do not explicitly use a super() in the child class?
Remember when the parent class does not have a default constructor the child class will give a compilation error when super() is not used.
Where is the super called?
Because a parent should exist before the creation of a child.