Bookmarked 1 If class D is derived from class C which is the
Bookmarked 1) If class D is derived from class C, which is the sub-class and the super class? Which class is the base class?
2) How do you stop a derived class from inheriting instance variables from a class you are designing?
3) Explain the difference between not declaring an access specifier and using the access specifier protected.
4) In a class declaration of a class B, how do you indicate that B is to be derived from class A?
5) How do you invoke a constructor of a base class from a direct sub-class?
6) What does overriding mean? When would you override a method?
7) Describe the difference between overriding and overloading.
8) What is meant by polymorphism in Java?
9) What are the public methods of the class Object? List all of them.
10) Explain what casting means when applied to reference variables and when it must be used.
11) Explain what the operator instanceof does.
12) What advantages does an abstract class offer? i.e., why would you create one?
13) What is the effect of declaring a method as final? of declaring a class as final? A variable? A parameter?
14) What is the difference between private and protected? Be specific in terms of inheritance.
Solution
1) If class D is derived from class C
As per inheritance concept the class which is deriving is sub class and the class from which its derived from is super class, hence D is sub class and C is super class.Super class is also called base class hence C is base class.
2) Make those instance variables private.
3) When we are not declaring any access specifier then by default the specifier it gets is public which means any one can access it but when we use protected access specifier then it means the variable, method(against which protected access specifier has been used) could be accessed via sub class in same package , its not accessible outside package.
4) For derivng any class extends keyword is used hence if B is to be derived from A then declaration would be like class B extends A
5) We can invoke constructor of base class directly from sub class using super() method
6) Overriding means a method which is already defined in base class and we are redefining the same method, same method name, parameters in sub class.
7) Overriding is defining the same method again in sub class and overloading is defining the same method with different definitions in same class (they will have different set of parameters)
8) Polymorphism is one name multiple form , overloading and overriding are implementation of polymorphism concept.
