Oop Concepts Part 2 - 1257
Oop Concepts Part 2 - 1257
Student Readout
OOP Concepts Part 2: Inheritance, Method
Overriding, and Polymorphism
Overview
This handout covers advanced Object-Oriented Programming (OOP) concepts in Java,
including Inheritance, Method Overriding, and Polymorphism. Understanding these concepts is
essential for writing efficient, reusable, and maintainable code.
1. Inheritance in Java
Definition
Inheritance allows a subclass to inherit properties and methods from a superclass, promoting
code reuse and establishing a hierarchical relationship between classes.
Types of Inheritance
1. Single Inheritance: A subclass inherits from one superclass.
Example:
class Animal {}
2. Multilevel Inheritance: A class is derived from a class that is also derived from another
class.
Example:
class Animal {}
class Dog extends Animal {}
Example:
class Animal {}
Animal (Superclass)
/ \
Dog Cat
(Subclass) (Subclass)
2. Method Overriding
Definition
Method Overriding occurs when a subclass provides a specific implementation of a method
already defined in its superclass. The method must have the same name, return type, and
parameters.
Examples
1. Basic Overriding:
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
2. Access Modifier:
class Animal {
@Override
System.out.println("Dog barks");
3. Final Method:
class Animal {
}
3. Polymorphism
Definition
Polymorphism allows one method to behave differently based on the object that is calling it. It
can be achieved through method overloading (compile-time polymorphism) or method
overriding (runtime polymorphism).
Types of Polymorphism
1. Compile-time Polymorphism (Method Overloading): Multiple methods with the same
name but different parameters.
Example:
class MathOperations {
return a + b;
return a + b;
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
@Override
void sound() {
System.out.println("Dog barks");
Examples
1. Compile-time Polymorphism:
class Printer {
2. Runtime Polymorphism:
class Shape {
void draw() {
System.out.println("Drawing a shape");
@Override
void draw() {
System.out.println("Drawing a circle");
3. Polymorphic Behavior:
class Animal {
void sound() {
System.out.println("Animal sound");
@Override
void sound() {
System.out.println("Cat meows");
Example:
Animal a = new Dog(); // Upcasting
Downcasting
Downcasting is when a superclass reference is cast back to a subclass reference. This must be
done explicitly and can throw a ClassCastException .
Example:
Examples
1. Upcasting:
class Animal {}
2. Downcasting:
class Animal {}
3. ClassCastException:
class Animal {}
Summary
Inheritance: Enables code reuse by allowing subclasses to inherit from superclasses.
Method Overriding: Allows subclasses to provide specific implementations of methods
defined in superclasses.
Polymorphism: Enables methods to behave differently based on the object calling them,
achieved through overloading and overriding.
Upcasting and Downcasting: Allow objects to be treated as instances of their superclass
or subclass, respectively.