Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Object Oriented Programming (OOP)

Inheritance and Polymorphism

Inheritance:

Inheritance is one of the core concepts of object-oriented programming. It allows a class (subclass/child class) to inherit properties and behaviors from another class (superclass/parent class). Inheritance promotes code reuse and establishes relationships between classes.

Syntax of Inheritance:

class ParentClass {
    // superclass members
}

class ChildClass extends ParentClass {
    // subclass members
}

Example of Inheritance in Java:

// Parent class (superclass)
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

// Child class (subclass) inheriting from Animal
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

In this example, the Dog class is a subclass of the Animal class. Dog inherits the sound() method from Animal. If you create a Dog object and call sound(), it will print "Dog barks".

Polymorphism:

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables flexibility in programming by allowing methods to work with objects of any subclass of the declared type. Java supports two types of polymorphism: compile-time polymorphism (method overloading) and runtime polymorphism (method overriding).

Runtime Polymorphism (Method Overriding):

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. It allows a subclass to provide a specialized version of a method that is already defined in its superclass.

Example of Method Overriding:

// Parent class (superclass)
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

// Child class (subclass) overriding the sound() method
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

In this example, the sound() method in the Dog class overrides the sound() method in the Animal class. When you call sound() on a Dog object, it prints "Dog barks" instead of "Animal makes a sound".

Example with Polymorphism:

public class PolymorphismExample {

    public static void main(String[] args) {
        Animal myDog = new Dog(); // Polymorphic statement
        myDog.sound(); // Output: Dog barks
    }
}

In this example, myDog is declared as an Animal type but is actually referring to a Dog object. This is an example of polymorphism. When sound() is called, it invokes the overridden method in the Dog class, demonstrating runtime polymorphism. This flexibility simplifies code and allows for more dynamic behavior in Java programs.

PreviousClasses and ObjectsNextEncapsulation and Abstraction

Last updated 1 year ago