Java Inheritance and Multithreading Updated
Java Inheritance and Multithreading Updated
• - Code reusability
• - Simplifies the application structure
• - Facilitates method overriding and
polymorphism
Example: Inheritance Syntax
• class Parent {
• void display() {
• System.out.println("Parent Class");
• }
• }
• - Single Inheritance
• - Multilevel Inheritance
• - Hierarchical Inheritance
• - (Note: Multiple inheritance via classes is not
supported)
Multithread Programming
• Harnessing the power of parallelism in Java.
What is Multithreading?
• Multithreading enables concurrent execution
of two or more threads to utilize CPU
efficiently.
• - Thread: Lightweight sub-process
• - Uses CPU cycles during I/O or resource-
waiting time
Lifecycle of a Thread
• class Main {
• public static void main(String[] args) {
• MyThread t = new MyThread();
Synchronization in Threads
• Ensures that multiple threads can access
shared resources safely.
• - Use the `synchronized` keyword
• - Prevents race conditions
• - Provides thread-safe operations
Inheritance and Polymorphism
• Inheritance plays a key role in enabling
polymorphism.
• - Polymorphism allows methods to perform
differently based on the object that invokes
them.
• - Method overriding demonstrates runtime
polymorphism.
Downcasting and Upcasting