Unit 3 - Lecture-7
Unit 3 - Lecture-7
1
Unit-3
• Inheritance:
• Inheritance in Java,
• Types,
• Constructor in Inheritance,
• Using final with Inheritance,
• Accessing superclass member,
• Override private methods,
• Parent and Child classes having same data member,
• Base vs derived class reference.
• Polymorphism:
• Method Overloading,
• Overloading main(),
• Static vs Dynamic Binding,
• Method Hiding.
• Private and final methods,
• Passing and Returning Objects in Java
Polymorphism
• Method Overloading,
• Overloading main(),
• Static vs Dynamic Binding, Method Hiding.
• Private and final methods,
• Passing and Returning Objects in Java
private and final methods in polymorphism
• In Java private methods are the methods having private access modifier and are restricted to be
access in the defining class only and are not visible in their child class due to which are not eligible
for overridden.
• However, we can define a method with the same name in the child class and could access in parent
class.
• Like private methods final methods in Java are the methods having final non-access modifier instead
of private and are again restricted to be accessed in the defining class only and are not visible in their
child class due to which are not eligible for overridden.
• The only difference between private and final methods is that in case of final methods we even can't
define a method with the same name in child class while in case of private methods we could define.
• In Java as both private and final methods do not allow the overridden functionality so no use of using
both modifiers together with same method.
public class PrivateFinalMethod {
private void print() {
System.out.println("in parent print");
}
public static void main(String[] args) {
op.changeVar(500);
System.out.println("after change "+op.data);
op.changeObj(500);
}
}
// Java program to demonstrate objects passing to methods.
class ObjectPassDemo
{
int a, b;
ObjectPassDemo(int i, int j)
{
a = i;
b = j;
}
// Driver class
public class Test
{
public static void main(String args[])
{
ObjectPassDemo ob1 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob2 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob3 = new ObjectPassDemo(-1, -1);