0% found this document useful (0 votes)
12 views

Java Inheritance and Multithreading Updated

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

Java Inheritance and Multithreading Updated

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

Java Inheritance Basics

• Understanding the foundation of inheritance


in Java.
What is Inheritance?
• Inheritance allows one class to acquire the
properties and behaviors of another class.
• - Promotes code reuse
• - Creates a parent-child relationship
• - Supports hierarchical classification
Key Terms in Inheritance

• - Superclass (Parent Class): The class being


inherited from.
• - Subclass (Child Class): The class that inherits
from the superclass.
• - Keyword `extends`: Used to establish
inheritance.
Benefits of Inheritance

• - Code reusability
• - Simplifies the application structure
• - Facilitates method overriding and
polymorphism
Example: Inheritance Syntax
• class Parent {
• void display() {
• System.out.println("Parent Class");
• }
• }

• class Child extends Parent {


• void display() {
• System.out.println("Child Class");
Types of Inheritance in Java

• - 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

• - New: Thread created but not started


• - Runnable: Thread is ready to run
• - Running: Thread is executing
• - Blocked/Waiting: Thread is paused for
resources
• - Terminated: Thread has completed execution
Creating Threads in Java
• Two main ways to create a thread:
• - Extend the `Thread` class
• - Implement the `Runnable` interface
Example: Creating Threads
• class MyThread extends Thread {
• public void run() {
• System.out.println("Thread is running");
• }
• }

• 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

• - Upcasting: Casting a subclass to a superclass


reference.
• - Downcasting: Casting a superclass reference
back to a subclass.
Conclusion: Java Inheritance
• Java inheritance simplifies code by reusing
existing functionality and building
relationships between classes.
• - Facilitates modular and maintainable code.
• - Enables polymorphism and dynamic method
dispatch.
• - Promotes the principle of DRY (Don't Repeat
Yourself).
Thank You
• Exploring Java Inheritance Basics
• Questions are welcome!
• - By: [Your Name]
Thread Priorities
• Java threads can have priorities to determine
the order of execution.
• - Thread priority ranges from 1
(MIN_PRIORITY) to 10 (MAX_PRIORITY).
• - Default priority is 5 (NORM_PRIORITY).
• - Higher-priority threads get more CPU time.
Thread Communication
• Threads in Java can communicate with each
other using:
• - `wait()`: Causes the current thread to wait
until another thread invokes `notify()` or
`notifyAll()`.
• - `notify()`: Wakes up a single thread that is
waiting on the object's monitor.
• - `notifyAll()`: Wakes up all threads waiting on
the object's monitor.
Common Thread Problems

• - Deadlock: Two or more threads waiting on


each other indefinitely.
• - Race condition: Multiple threads accessing
shared resources simultaneously without
proper synchronization.
• - Starvation: Threads with low priority waiting
indefinitely for resources.
Conclusion: Multithread
Programming
• Multithreading enhances performance by
executing multiple tasks concurrently.
• - Improves CPU utilization.
• - Enables efficient program design for real-
world applications like servers and GUIs.
• - Proper synchronization ensures thread safety
and reliability.
Thank You
• Diving into Multithread Programming
• Questions are welcome!
• - By: [Your Name]

You might also like