DMCA.com Protection Status Java Programming Lesson - 12 | Inheritance & Polymorphism — The Heart of Object-Oriented Programming in Java - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson - 12 | Inheritance & Polymorphism — The Heart of Object-Oriented Programming in Java

Java Programming Lesson - 12

Welcome to Lesson-12: Inheritance & Polymorphism — the heart of Object-Oriented Programming in Java.

These concepts allow us to reuse code and write flexible, scalable programs.


📘 Lesson-12 — Inheritance & Polymorphism


✅ 1️⃣ What is Inheritance?

Inheritance allows one class to acquire the properties and methods of another class.

  • Parent Class (Super Class) → Base class

  • Child Class (Sub Class) → Derived class

Syntax:

class Parent { int x = 10; void show() { System.out.println("Parent method"); } } class Child extends Parent { void display() { System.out.println("Child method"); } }

Example:

public class Main { public static void main(String[] args) { Child c = new Child(); System.out.println("x = " + c.x); // inherited c.show(); // inherited c.display(); // own method } }

✅ Output:

x = 10 Parent method Child method

✅ 2️⃣ Types of Inheritance in Java

TypeDescription
Single               One child inherits one parent
Multilevel               Grandparent → Parent → Child
Hierarchical               One parent → multiple children

📌 Java does NOT support multiple inheritance with classes directly (use interfaces instead)


✅ 3️⃣ What is Polymorphism?

Polymorphism = "Many Forms"

  • Allows objects to behave differently based on context

  • Types:

    1. Compile-time / Method Overloading

    2. Runtime / Method Overriding


✅ 3.1 Method Overloading (Compile-time)

Same method name → Different parameters

class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } } public class Main { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 10)); // 15 System.out.println(calc.add(5, 10, 15)); // 30 } }

✅ 3.2 Method Overriding (Runtime)

Child class redefines a method of parent class

class Animal { void sound() { System.out.println("Animal makes sound"); } } class Dog extends Animal { void sound() { // overriding System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Animal a = new Animal(); a.sound(); // Animal makes sound Dog d = new Dog(); d.sound(); // Dog barks } }

✅ Key Points

✔ Inheritance → code reuse
✔ Polymorphism → flexibility / multiple forms
✔ Overloading → Compile-time
✔ Overriding → Runtime


📝 Exercise for You ✅

Create a program:

1️⃣ Parent class: Shape → method area()
2️⃣ Child classes: Circle (radius), Rectangle (length, width)
3️⃣ Override area() in both children → print area
4️⃣ Demonstrate polymorphism by calling area() for each object

Example Output:

Circle area: 78.5 Rectangle area: 50

Reply with:
✅ Code
✅ Output

I will check & give feedback 🔥


🎯 Quick Quiz

1️⃣ Which keyword is used to inherit a class?
A) implements
B) extends
C) inherit

2️⃣ Polymorphism allows:
A) One form only
B) Multiple forms
C) No forms

Reply like:
1 → ?, 2 → ?


✅ Lesson-12 Completed!


NEXT PARTAdvanced Java Topics — this is where you go from “Java beginner” to pro-level developer.


📘 Advanced Java Topics Roadmap

1️⃣ Collections Framework

  • Lists (ArrayList, LinkedList)

  • Sets (HashSet, TreeSet)

  • Maps (HashMap, TreeMap)

  • Iterators & Loops

2️⃣ Exception Handling Deep Dive

  • Custom Exceptions

  • Multiple catch, try-with-resources

3️⃣ Multithreading & Concurrency

  • Thread class & Runnable interface

  • Thread states, sleep, join

  • Synchronization

4️⃣ File I/O

  • Reading & Writing Files (Text/Binary)

  • BufferedReader, BufferedWriter

  • File class & Paths

5️⃣ Java Generics

  • Type-safe Collections

  • Generic methods & classes

6️⃣ Java Streams & Lambda Expressions

  • Functional programming in Java

  • Filtering, mapping, reducing collections

7️⃣ Java Networking (Optional Advanced)

  • Sockets, HTTP requests

  • Client-Server programming


Do You Ready To Advanced JAVA PROGRAMMING Learn Comment Under The Post.

Frequently Asked Questions

What is inheritance in Java?

• Inheritance allows a child class to use parent class features.
• Supports reusability and reduces duplicate code.

What is method overriding?

• Subclass redefines a parent class method.
• Used to achieve runtime polymorphism.

What is polymorphism?

• One method behaving differently based on the object.
• Types: Compile-time and Runtime.

No comments

Thank You For Visit My Website.
I Will Contact As Soon As Possible.

Theme images by 5ugarless. Powered by Blogger.