0% found this document useful (0 votes)
22 views

Notes

The document discusses inheritance in Java including abstract classes, abstract methods, and polymorphism. It provides examples of final classes, final methods, and overriding vs overloading. It also lists YouTube videos about these topics and sample code demonstrating abstract classes and methods. The code defines an abstract Shape class with an abstract area() method. Circle, Rectangle, and Triangle concrete classes extend Shape and override the area() method.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Notes

The document discusses inheritance in Java including abstract classes, abstract methods, and polymorphism. It provides examples of final classes, final methods, and overriding vs overloading. It also lists YouTube videos about these topics and sample code demonstrating abstract classes and methods. The code defines an abstract Shape class with an abstract area() method. Circle, Rectangle, and Triangle concrete classes extend Shape and override the area() method.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lecture 10-Oct-2020 Saturday

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

ii) Fn Overloading Vs Overriding


Hindi:https://youtu.be/DNV7fxW68Iw
Sindhi:https://youtu.be/8deHXvhXyxU
English: https://youtu.be/8JmBaLMg9lw

iii) Everything on Inheritance in Java जानिए सबकु छ जावा मे ईनहेरिटेंस के बारे मे


Link: https://youtu.be/epU1ylOKLQk
-----------------------------------------------------------------------------------
------------------
1. Discussion of previous class OCJP Questions.
Given on 06-Oct-2020
OCJP1: What is the prototype/declaration of finalize method in Java?
Google Search
Ans: protected void finalize() throws java.lang.Throwable
Hint: javap java.lang.Object
OCJP2: In which Java class, finalize method is already defined?
Google Search
Ans: java.lang.Object class - Imp: In Java, Object class is by default parent of
all classes.
OCJP3. Which exception is thrown by finalize method?
Google Search
Ans: Throwable [ java.lang.Throwable ]
OCJP4. What is access specifier/visibility mode of finalize method?
Google Search
(a) private (b) proteced (c) public (d) none

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

Example: OCJP1: Which of the following is INVALID?


i) void area(int r);
ii) void area(int l, int b); //Point (a)
iii) void area(float r); //Point (b)
iv) double area(double r); //Point b, c
v) double area(int r);
Ans: (v) is INVALID.
Reason: For Valid Overloading either point (a) or point (b) must be fulfilled.
Prototype (v) is same prototype (I) and only point (c) is satisfied but it
will NOT play any role

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.

More on Overloading Vs Overriding CwB [Video]

Example Program based on Fn Overloading and Overriding [EQ]

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

OCJP4. Try following combinations in above program in place of modifiers in (*1)


and (*2) compile, run and note down result/error

(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 - Compile Time Error in Line 3.


Reason: In Java, we can restrict further inheritance of some class by declaring it
as "final".
In above code class B is "final". So class C cannot inherit final class B
OCJP10. Concept of "final method"
class A{
public final void show(){ } //(*4)
}
class B extends A{
public void show(){ } //(*5)
}
Filename: FinalMethod.java
Compile: javac FinalMethod.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]

OCJP11. Concept of "final variable"


public class FinalVariable{
public static void main(String args[ ]){
final double PI=3.14;
PI=4.14; //(*6)
System.out.println("PI=" + PI);
}
}
Filename: A.java
Compile: javac A.java
Ans: Error at Line (*6). Reason - "final" variables are just like constants and can
only be initialized only once.
Where as in Line (*6) we are trying to re-assign another value to it
-----------------------------------------------------------------------------------
------------------------
4. Concept of abstract class, abstract method, hierarchical inheritance and RTP
(Runtime Polymorphism). - Interview [CwB]

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{ }

=>It may also have non-abstract (normal) methods [OCJP6 - Imp]


Example:
public void category(){ }

=>We cannot instantiate object of abstract class using "new" - OCJP9 [Imp]
Example:
Shape s=new Shape();

=>Reference variables of astract class are allowed [OCJP10] i.e


ClassName variablename;
Example:
Shape s;
=>If child class do not provide definition of parent class abstract method then it
will also became an "abstract class" and we need to put "abstract" keyword before
that class otherwise code cannot compile [OCJP11] [In our case Triangle class]
Example:
abstract class Triangle extends Shape{ } //Empty Code

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);

Example: [Interview Question- Give Example of abstract class]

abstract class Shape{ //OCJP5 - abstract keyword - ASG


public abstract void area(); //abstract method
public void category() { System.out.println("Category is Shape"); } //OCJP6
}
class Circle extends Shape{ //VK
public void area(){ System.out.println("Area of Circle=pi*r*r"); } //OCJP7-
Override
}
class Rectangle extends Shape{ //NC
public void area(){ System.out.println("Area of Rectangle=l*b");} //OCJP7-Override
}
class Triangle extends Shape{ //LT - OCJP11
//No Code is specified here (*7) -OCJP 8 CTE
}
public class AbstractShapeExample{
public static void main(String args[ ]);{
// Shape s=new Shape(); //OCJP 9 CTE - Cannot instantiate abstract class
Shape s; //OCJP10 - Reference Variable are allowed
Circle c=new Circle();
Rectangle r=new Rectangle();
// Triangle t=new Triangle(); - abstract class cannot be instantiate
//Message Passing
c.area(); r.area();
}
}

OCJP8: What happens if we do not write anything in place of (*7)


Ans: Error - Since it is compulsory for the child class to provide definition of
abstract method of the parent class.
This is the way to achievement "code enforcement".

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

You might also like