Unit-4 PPL
Unit-4 PPL
An abstract data type is an abstraction of a data structure that provides only the interface to which
the data structure must adhere. The interface does not give any specific details about something
should be implemented or in what programming language.
T he above figure shows the ADT model. There are two types of models in the ADT model, i.e., the
public function and the private function. The ADT model also contains the data structures that we
are using in a program. In this model, first encapsulation is performed, i.e., all the data is wrapped in
a single unit, i.e., ADT. Then, the abstraction is performed means showing the operations that can be
performed on the data structure and what are the data structures that we are using in a program.
Types of ADT
ADTs allow users to effectively implement complex mathematical
and logical programs as they are flexible and independent of
language. There are three types of ADTs.
1. List
2. Queue
3. Stack
1. List: The list ADT is a sequential combination of one or more than one element. Each
element has its positional value and a referential pointer to the next element. Users can't
access elements randomly, so the head pointer is initialized to the first element of the list.
Basic operations of the list are as follows:
1
PPL Unit-4
2. Queue: The queue is a linear ADT which allows insertion from one end and deletion from
another. As per the name, It works on the FIFO (first in, first out) principle. The queue has
two ends, front and rear. The rear end is used for insertion, and the front end is used for
deletion.
The basic operations of the queue are as follows:
3. Stack: A stack is a linear ADT that restricts users from inserting and deleting elements from
only one end, known as the top of the stack. It uses LIFO (last in, first out) as its basic
working principle. The top of the queue always has a recently inserted element.
Basic operations of the stack are as follows:
OOPs Concepts
Object-Oriented Programming is a methodology or paradigm to design a program using classes and
objects.
Features of OOPs
1. Object: Any entity that has state and behavior is
known as an object. An Object can be defined as an
instance of a class. An object contains an address and
takes up some space in memory.
2
PPL Unit-4
3. Inheritance: When one object acquires all the properties and behaviours of a parent object, it is
known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
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.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
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.
iii. Hierarchical Inheritance: When two or more classes inherits a single class, it is
known as hierarchical inheritance. In the example given below, Dog and Cat
classes inherits the Animal class, so there is hierarchical inheritance.
Example: class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
v. Hybrid inheritance: The process of combining more than one type of Inheritance together while
deriving subclasses in a program is called a Hybrid Inheritance.
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.
4
PPL Unit-4
i. Abstract class: A class which is declared as abstract is known as an abstract class. It can have
abstract and non-abstract methods. It needs to be extended and its method implemented. It
cannot be instantiated.
Point to be remembered
An abstract class must be declared with an abstract keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not to change the body of the
method.
Syntax: abstract class A{}
Abstract Method: A method which is declared as abstract and does not have implementation is
known as an abstract method.
Example:
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");} output:
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
ii. Interface:
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
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.
Java Interface also represents the IS-A relationship.
It cannot be instantiated just like the abstract class.
An interface is declared by using the interface keyword.
Example:
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
5. Encapsulation: Encapsulation in Java is a process of wrapping code and data together into a
single unit, for example, a capsule which is mixed of several medicines.
We can create a fully encapsulated class in Java by making all the data members of the class private.
Now we can use setter and getter methods to set and get the data in it.
5
PPL Unit-4
Method Overloading: If a class has multiple methods having same name but different in parameters,
it is known as Method Overloading. If we have to perform only one operation, having same name of
the methods increases the readability of the program.
Example1:
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Example2:
class Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
6
PPL Unit-4
}}
Method Overriding: If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been
declared by one of its parent class, it is known as method overriding.
Example:
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike extends Vehicle{
public static void main(String args[]){
//creating an instance of child class
Bike obj = new Bike(); Output:
//calling the method with child class instance
obj.run();
}
}
Fragmentation
Fragmentation is an unwanted problem in the operating system in which the processes are loaded
and unloaded from memory, and free memory space is fragmented. Processes can't be assigned to
memory blocks due to their small size, and the memory blocks stay unused.
Types of Fragmentation
1. Internal Fragmentation: When a process is allocated to a memory block, and if the process is
smaller than the amount of memory requested, a free space is created in the given memory
block. Due to this, the free space of the memory block is unused, which causes internal
fragmentation.
For Example: Assume that memory allocation in RAM is done using fixed partitioning (i.e., memory
blocks of fixed sizes). 2MB, 4MB, 4MB, and 8MB are the available sizes. The Operating System uses a
part of this RAM.
Let's suppose a process P1 with a size of 3MB arrives and is given a memory block of 4MB. As a
result, the 1MB of free space in this block is unused and cannot be used to allocate memory to
another process. It is known as internal fragmentation.
2. External Fragmentation: External fragmentation happens when a dynamic memory
allocation method allocates some memory but leaves a small amount of memory unusable.
The quantity of available memory is substantially reduced if there is too much external
fragmentation. There is enough memory space to complete a request, but it is not
contiguous. It's known as external fragmentation.
For Example:
Let's take the example of external fragmentation. In the above diagram, you can see that there is
sufficient space (50 KB) to run a process (05) (need 45KB), but the memory is not contiguous. You can
use compaction, paging, and segmentation to use the free space to execute a process.
8
PPL Unit-4
Storage Allocation:
Storage management is a data storage technique which automatically moves data between high-cost
and low-cost storage media.
The different ways to allocate memory are:
1. Static storage allocation(fixed location, fixed size)
2. Stack storage allocation(variable location, fixed size)
3. Heap storage allocation(variable location, variable size)
1. Static storage allocation:
In static allocation, names are bound to storage locations.
If memory is created at compile time then the memory will be created in static area and only
once.
Static allocation supports the dynamic data structure that means memory is created only at
compile time and deallocated after program completion.
The drawback with static storage allocation is that the size and position of data objects
should be known at compile time.
Another drawback is restriction of the recursion procedure.
9
PPL Unit-4
10
PPL Unit-4
ii. Fragmentation: Variable size heap storage can help reduce fragmentation compared to fixed
size heaps, as memory can be allocated in sizes that best fit the data structures.
iii. Allocation Overhead: Managing variable-sized blocks can introduce some overhead, as the
allocator needs to keep track of available memory blocks and manage their sizes.
iv. Examples: Common memory allocation functions in languages like C and C++ (e.g., malloc,
calloc, realloc), as well as more advanced memory management techniques used in modern
programming languages.
Garbage Collection
Garbage Collection is process of reclaiming the runtime unused memory automatically. In other
words, it is a way to destroy the unused objects.
To do so, we were using free() function in C language and delete() in C++. But, in java it is performed
automatically. So, java provides better memory management.
It helps prevent memory leaks, where memory is allocated but not properly deallocated, leading to
inefficient memory usage and potential program instability. Garbage collection is particularly
important in languages that offer dynamic memory allocation.
Advantage of Garbage Collection
It makes memory efficient because garbage collector removes the unreferenced objects
from heap memory.
It is automatically done by the garbage collector(a part of JVM) so we don't need to make
extra efforts.
11
PPL Unit-4
collection. Parallel GC can use multiple CPUs to speed up the application throughput. So, it is also
known as throughput collector. It is used if we want to execute a long process (like batch
processing) and where long pauses are acceptable. If you want to use the parallel garbage
collector, execute the -XX:+UseParallelGC JVM argument to activate it.
G1 GC shows a concurrent global marking phase to determine the live and dead objects throughout
the heap. After the completion of the mark phase, G1 collects the information of regions that
contains the most garbage objects. After that these regions are swept first. If you want to use the G1
garbage collector, execute the -XX:+UseG1GC JVM argument to activate it.
Stop the World Event
It is a situation when the garbage collector performs the garbage collection (GC) and stops all the
application's threads until the GC process is not completed. The process is known as Stop the World
(STW) events.
12