Unit 4 Multithreading.pptx
Unit 4 Multithreading.pptx
Inheritance
Introduction
Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object.
The idea behind inheritance in Java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields
of the parent class. Moreover, you can add new methods and fields in your current class
also.
The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality.
Example 1
class Animal {
}
Example 2:
class Employee{
float salary=40000;
String name=”ABC”;
Void display()
{
System.out. println(“Name of employee: “+name);
}
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
p.display();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Types of inheritance
Types of Inheritance
Example
Programs reference Practical 4
Method overriding
If methods with the same name are defined in both superclass and subclass, the
method in the subclass overrides the method in the superclass. This is called
method overriding.
class Animal {
public void printMessage(){
// overridden method display();
public void display(){ }
System.out.println("I am an animal"); }
}
} class Main {
public static void main(String[] args) {
class Dog extends Animal { Dog dog1 = new Dog();
dog1.printMessage();
// overriding method }
@Override }
public void display(){
System.out.println("I am a dog");
}
Super keyword
The super keyword in Java is used in subclasses to access superclass members (attributes,
constructors and methods).
The super keyword in Java is a reference variable which is used to refer immediate parent
class object.
}}
super is used to refer immediate parent class instance variable.
The superclass and subclass can have attributes with the same name. We use the super keyword to access
the attribute of the superclass.
class Animal {
protected String type="animal"; class Main {
} public static void main(String[] args) {
Dog dog1 = new Dog();
class Dog extends Animal { dog1.printType();
public String type="mammal"; }
}
public void printType() {
System.out.println("I am a " + type);
System.out.println("I am an " + super.type);
}
}
Abstract Classes and Interfaces
Abstraction in Java
Abstraction is a process of hiding the implementation details and showing only
functionality to the user.
Another way, it shows only essential things to the user and hides the internal
details, for example, sending SMS where you type the text and send the
message. You don't know the internal processing about the message delivery.
// abstract method
// regular method
void method2() {
}
Abstract methods
A method that doesn't have its body is known as an abstract method. We use the same
abstract keyword to create abstract methods. For example,
If a class contains an abstract method, then the class should be declared abstract.
Otherwise, it will generate an error. For example,
// error
class Language {
// abstract method
}
Program :
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run()
{System.out.println("running safely");}
public static void main(String args[]){
Honda4 obj = new Honda4();
obj.run();
} }
Program 2:
abstract class Shape{
class TestAbstraction1{
s.draw();
} }
What is an Interface?
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java.
In other words, you can say that interfaces can have abstract methods and variables. It
cannot have a method body.
// by default.
}
Relationship between classes and interfaces
Example:
interface printable{
void print();
obj.print();
}
Why Multiple Inheritance is not supported in Java?
Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit
properties of more than one parent class.
The problem occurs when there exist methods with the same signature in both the
superclasses and subclass.
On calling the method, the compiler cannot determine which class method to be called
and even on calling which class method gets the priority.
Consider a case where class B extends class A and Class C and both class A and C have the
same method display().
Now java compiler cannot decide, which display method it should inherit. To prevent
such situation, multiple inheritances is not allowed in java.
Program:
// Multiple Inheritance using Interface
// CNG Car interface
interface CNG_Car{ class Hybrid_Car implements Petrol_Car,
CNG_Car {
// Abstract methods
public void drive(){
void drive();
System.out.println("Driving a Hybrid Car");
void cng_kit();
}
}
// Overridden method of CNG_Car Interface
// Petrol Car interface
interface Petrol_Car{ public void cng_kit(){
}}
class MultipleInheri {
Hybrid_Car obj = new Hybrid_Car(); // Creating a new object of the Hybrid Car class
obj.drive();
obj.cng_kit();
obj.petrol_kit();
}
Difference between Interface and an Abstract Class
To be done by students, any 6 points of difference.
Multithreading
Introduction
A Thread is a very light-weighted process, or we can say the smallest part of the
process that allows a program to operate more efficiently by running multiple
tasks simultaneously.
In order to perform complicated tasks in the background, we used the Thread
concept in Java. All the tasks are executed without affecting the main program.
In a program or process, all the threads have their own separate path for
execution, so each thread of a process is independent.
Another benefit of using thread is that if a thread gets an exception or an error
at the time of its execution, it doesn't affect the execution of the other threads.
All the threads share a common memory and have their own stack, local
variables and program counter. When multiple threads are executed in parallel at
the same time, this process is known as Multithreading.
Thread life cycle
During the lifetime of a thread, there are many states it can enter. They include:
1. Newborn state
2. Runnable state
3. Running state
4. Blocked state
5. Dead state
A thread is always in one of these five states. It can move from one state to
another via a variety of ways as shown in figure.
Thread control methods
Core Java provides complete control over multithreaded program. You can develop a
multithreaded program which can be suspended, resumed, or stopped completely based
on your requirements.
1. public void suspend() - This method puts a thread in the suspended state and can be
resumed using resume() method.
2. public void stop() - This method stops a thread completely.
3. public void resume() - This method resumes a thread, which was suspended using
suspend() method.
4. public void wait() - Causes the current thread to wait until another thread invokes
the notify().
5. public void notify() - Wakes up a single thread that is waiting on this object's
The main thread
Creating a thread
Extending the thread class
Example : refer Practical 7B
Stopping and Blocking a thread
Using thread methods