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

Unit 3 - Lecture-4

This document discusses object-oriented programming concepts in Java including inheritance, polymorphism, hiding fields and methods, and parent and child classes having the same data member. It provides examples of inheritance in Java including constructor usage, accessing superclass members, overriding and hiding methods. It also discusses static vs dynamic binding, method overloading, and passing and returning objects. Examples are given for hiding fields and methods as well as parent and child classes having the same data member value.

Uploaded by

VORTEX GAMING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Unit 3 - Lecture-4

This document discusses object-oriented programming concepts in Java including inheritance, polymorphism, hiding fields and methods, and parent and child classes having the same data member. It provides examples of inheritance in Java including constructor usage, accessing superclass members, overriding and hiding methods. It also discusses static vs dynamic binding, method overloading, and passing and returning objects. Examples are given for hiding fields and methods as well as parent and child classes having the same data member value.

Uploaded by

VORTEX GAMING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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
Hiding Fields & Methods
• Java, if you are not careful you can possibly hide both methods and variables of the
superclass.
• A field or variable is said to hide all fields with the same name in superclasses.
Similarly, a static method with the same name in a superclass can hide the method of
the subclass.
Method Hiding
• Method hiding is closely related to method overriding and sometimes programmer
hides the method trying to override it. The concept of overriding allows you to write
code which behaves differently depending upon an object at runtime.
• Static method with the same name in a superclass can hide the method from
subclass because a static method cannot be overridden in Java.
• This example, we assume that p.sleep() will call the sleep() method
from Parent class and c.sleep() will call the sleep() method from child class,
just like it happens in overriding but because sleep() is a static method, instead
of overriding we have hidden the sleep() method.

• Since static method are resolved at compile-time, both


are p.sleep() and c.sleep() are resolved to sleep() method of Parent class
because the type of p and c variable is Parent.
• If you remove the static keyword from both methods then it will behave as
expected.
Variable Hiding/Field Hiding
• A field or variable with the same name in subclass and
superclass is known as variable hiding or field hiding in Java.
When a variable is hidden, you can use super.variableName to
access the value from a superclass.

• you can access the superclass value using super.age.


Parent and Child classes having same data member in Java

• The parent class can hold reference to both the parent and child objects.
• If a parent class variable holds reference of the child class, and the value is present in both the
classes, in general, the reference belongs to the parent class variable.
• This is due to the run-time polymorphism characteristic in Java
class Demo_base {
int value = 1000;
Demo_base()
{
System.out.println("This is the base class constructor");
}
}

class Demo_inherits extends Demo_base {


int value = 10;
Demo_inherits()
{
System.out.println("This is the inherited class constructor");
}
}

public class Demo {


public static void main(String[] args) {
Demo_inherits my_inherited_obj = new Demo_inherits();
System.out.println("In the main class, inherited class object has been created");
System.out.println("Reference of inherited class type :" + my_inherited_obj.value);
Demo_base my_obj = my_inherited_obj;
System.out.println("In the main class, parent class object has been created");
System.out.println("Reference of base class type : " + my_obj.value);
}
}
Explanation
• A class named ‘Demo_base’ contains an integer value and a constructor that prints relevant
message. Another class named ‘Demo_inherits’ becomes the child class to parent class ‘Demo_base’.
• This means, the ‘Demo_inherits’ class extends to the base class ‘Demo_base’.
• In this class, another value for the same variable is defined, and a constructor of the child class is
defined with relevant message to be printed on the screen.
• A class named Demo contains the main function, where an instance of the child class is created, and
its associated integer value is printed on the screen.
• A similar process is done for the parent class, by creating an instance and accessing its associated
value and printing it on the screen.

You might also like