You are on page 1of 9

Object Oriented Programming

Prof. Rahul B. Diwate

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

PrivateFinalMethod obj = new PrivateFinalMethodsChild();


obj.print();
PrivateFinalMethodsChild obj1 = new PrivateFinalMethodsChild();
obj1.print();
}
}
class PrivateFinalMethodsChild extends PrivateFinalMethod {
public void print(){
System.out.println("in child print method");
}
}
Passing and Returning Objects in Java
• Reference in Java holds the memory location of object created if it is assigned to
that reference otherwise it is initiated as null.
• Now the point to remember here is that the value of the reference is the memory
location of the assigned object, so whenever we pass the reference to any method
as argument then we actually pass the memory location of that object which is
assigned to that particular reference.
• This technically means that the target method has memory location of our created
object and can access it to.
• So in case if target method access our object and make changes to any of property
of it than we would encounter with the changed value in our original object.
public class PassByValue {
static int k =10;
static void passPrimitive(int j) {
System.out.println("the value of passed primitive is " + j);
j = j + 1;
}
static void passReference(EmployeeTest emp) {
EmployeeTest reference = emp;
System.out.println("the value of name property of our object is "+ emp.getName());
reference.setName("Bond");
}
public static void main(String[] args) {
EmployeeTest ref = new EmployeeTest();
ref.setName("James");
passPrimitive(k);
System.out.println("Value of primitive after get passed to method is "+ k);
passReference(ref);
System.out.println("Value of property of object after reference get passed to method is "+ ref.getName());
} }
class EmployeeTest {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
class Operation{
int data=50;

void changeVar(int data){


// Changes will be in the local variable only
data=data+100;
}

void changeObj(int data){


//Changes will be in the instance variable
this.data=this.data+data;
}

public static void main(String args[]){


Operation op=new Operation();

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

// return true if o is equal to the invoking


// object notice an object is passed as an
// argument to method
boolean equalTo(ObjectPassDemo o)
{
return (o.a == a && o.b == b);
}
}

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

System.out.println("ob1 == ob2: " + ob1.equalTo(ob2));


System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));
}

You might also like