Notes
Notes
Topics to Cover:
Inheritance in Java
1. Discussion of previous class OCJP Questions.
2. Definition and Example program of Fn Overloading and Overriding
3. Concept of "final class" , "final method" and "final variable"
4. Concept of abstract class, abstract method, hierarchical inheritance and RTP
(Runtime Polymorphism). - Interview [CwB]
Videos to Watch on CwB
i) Virtual Function, Pure Virtual Function, Abstract Class and Runtime Polymorphism
Hindi: https://youtu.be/wnN9JKdRIbo
Sindhi:https://youtu.be/VvzjXMg74zw
English: https://youtu.be/TX8H8Vny5KA
Ans: (b) protected [Means only sub-classes of Object class can call this method]
non sub-classes cannot call it.
Given on 08-Oct-2020
OCJP8. What is the difference between Fn overloading and Fn Overriding
Ans:
Fn Overloading - [C++, Java, C#]
Multiple definition of the fn by changing
(a) Number of parameters
OR
(b) Datatype of parameters
(c) Imp: Return type will NOT play any role in Fn Overloading
Fn Overriding -
Definition - Redefining parent class fn inside its child class with same prototype,
means
Same fn Name, same parameters and same returntype.
OCJP2: Why same prototype?
Ans: Since, if change the prototype it becames "overloading" of parent class fn.
class A{
public void show(){ System.out.println("A1"); } //(*1)
public void show(int x){ System.out.println("A2"); } //Fn Overloading
}
class B extends A{
public void show(){ System.out.println("B1"); } //(*2) Fn Overriding
public void show(double x){ System.out.println("B2"); } //Fn Overloading
}
public class OverloadingOverridingExample{
public static void main(String args[ ]){
//Instantiation
A obj1=new A(); //Calls "Do Nothing Default Constructor"
obj1.show();
obj1.show(5);
obj1.show(5.5); //Error (*3)
B obj2=new B();
obj2.show();
obj2.show(5);
obj2.show(1.1);
}
}
OCJP3. What improvement we should do in place of (*3) so that the program get
compiled successfully?
Hint: See the Error Message
Ans: obj1.show((int)5.5); //Means typecast double value to int
(a) (b)
(*1) => protected (*1) => public
(*2) => public (*2) => protected
08-Oct-2020
Try following code, Compile and try to run - Note Down Result/Error.
OCJP9. Concept of "final class"
class A{ } //Empty class - Valid //Line 1
final class B extends A{ } //Line 2
class C extends B{ } //Line 3
Filename: FinalClass.java
Compile: javac FinalClass.java
Ans: CTE at Line (*5) - Reason: It is NOT possible for the child class to
"Override" final method of the parent class.
[To restrict/stop child class from overriding certain methods]
Abstract class
Definition: A class, which may contain 'one or more abstract methods' .
=>It is must to specify "abstract" keyword before the classname. [OCJP 5]
Example:
abstract class Shape{ }
=>We cannot instantiate object of abstract class using "new" - OCJP9 [Imp]
Example:
Shape s=new Shape();
Abstract method:
Definition: Only declaration of these methods are specified in the class and it is
compulsory for the child class to give definition (means override them). [OCJP7]
Syntax:
public abstract returntype functionname(datatype parameters);
Todo: Based on this example can we can create some other programs as
Abstract Class Abstract Method Concrete child class extends it
inside it
i) Shape area(); Circle, Rectangle, Triangle
ii) Animal sound(); Dog, Cat, Horse
iii) Fruit taste(); Mango, Orange