0% found this document useful (0 votes)
11 views11 pages

Oop Concepts Part 2 - 1257

This document provides an overview of advanced Object-Oriented Programming (OOP) concepts in Java, focusing on Inheritance, Method Overriding, and Polymorphism. It explains the types of inheritance, rules for method overriding, and the differences between compile-time and runtime polymorphism, along with examples. Additionally, it covers upcasting and downcasting, emphasizing their importance in Java application development.

Uploaded by

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

Oop Concepts Part 2 - 1257

This document provides an overview of advanced Object-Oriented Programming (OOP) concepts in Java, focusing on Inheritance, Method Overriding, and Polymorphism. It explains the types of inheritance, rules for method overriding, and the differences between compile-time and runtime polymorphism, along with examples. Additionally, it covers upcasting and downcasting, emphasizing their importance in Java application development.

Uploaded by

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

5.2.

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

class Dog extends 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 {}

class Puppy extends Dog {}

3. Hierarchical Inheritance: Multiple classes inherit from a single superclass.

Example:

class Animal {}

class Dog extends Animal {}

class Cat extends Animal {}

Diagram: Inheritance Hierarchy

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.

Rules for Method Overriding


1. Same Method Signature: The method in the subclass must match the method in the
superclass.
2. Access Modifier: The overriding method cannot be more restrictive than the superclass
method.
3. Use of @Override Annotation: Recommended for clarity and error checking.
4. Cannot Override final Methods: Methods marked as final cannot be overridden.

Examples
1. Basic Overriding:

class Animal {

void sound() {

System.out.println("Animal makes a sound");

class Dog extends Animal {

@Override

void sound() {

System.out.println("Dog barks");

2. Access Modifier:

class Animal {

public void sound() {


System.out.println("Animal makes a sound");

class Dog extends Animal {

@Override

public void sound() {

System.out.println("Dog barks");

3. Final Method:

class Animal {

final void sound() {

System.out.println("Animal makes a sound");

class Dog extends Animal {

// Cannot override sound() because it is final in 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 {

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

2. Runtime Polymorphism (Method Overriding): Subclass overrides a method from its


superclass.

Example:

class Animal {

void sound() {
System.out.println("Animal makes a sound");

class Dog extends Animal {

@Override

void sound() {

System.out.println("Dog barks");

Animal a = new Dog();

a.sound(); // Output: Dog barks

Examples
1. Compile-time Polymorphism:

class Printer {

void print(int num) {

System.out.println("Printing number: " + num);

void print(String text) {


System.out.println("Printing text: " + text);

2. Runtime Polymorphism:

class Shape {

void draw() {

System.out.println("Drawing a shape");

class Circle extends Shape {

@Override

void draw() {

System.out.println("Drawing a circle");

Shape s = new Circle();

s.draw(); // Output: Drawing a circle

3. Polymorphic Behavior:
class Animal {

void sound() {

System.out.println("Animal sound");

class Cat extends Animal {

@Override

void sound() {

System.out.println("Cat meows");

Animal a = new Cat();

a.sound(); // Output: Cat meows

4. Upcasting and Downcasting


Upcasting
Upcasting is when a subclass object is treated as an instance of its superclass. This is done
implicitly.

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:

Animal a = new Dog(); // Upcasting

Dog d = (Dog) a; // Downcasting

Examples
1. Upcasting:

class Animal {}

class Dog extends Animal {}

Animal a = new Dog(); // Implicit upcasting

2. Downcasting:

class Animal {}

class Dog extends Animal {}

Animal a = new Dog(); // Upcasting


Dog d = (Dog) a; // Explicit downcasting

3. ClassCastException:

class Animal {}

class Dog extends Animal {}

class Cat extends Animal {}

Animal a = new Cat();

Dog d = (Dog) a; // Throws ClassCastException

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.

Potential Gaps or Unclear Points


Difference between Overloading and Overriding: Overloading involves methods with
the same name but different parameters, while overriding involves redefining a method in
a subclass.
Upcasting and Downcasting: Upcasting is implicit and safe, while downcasting must be
explicit and can throw exceptions.
Final Thoughts
Understanding these advanced OOP concepts is crucial for developing robust Java
applications. If you have any questions or need further clarification, feel free to ask!

You might also like