CS23D017 Oops Project
CS23D017 Oops Project
CS23D017 Oops Project
On
“Inheritance, Multithreading and
Constructor Overloading”
Submitted By:
Sachin Hanaslal Rahangdale
Guided By:
Mr. Yogesh Katre
2023-2024
© S.B.J.I.T.M.R Nagpur 2023
S.B. JAIN INSTITUTE OF TECHNOLOGY MANAGEMENT AND
RESEARCH, NAGPUR
(An Autonomous Institute, Affiliated to RTMNU, Nagpur)
SESSION 2023-2024
CERTIFICATE
This is to certify that the Project titled “Project Title” is a bonafide work of Student Name
“Sachin H. Rahangdale” carried out for the partial fulfillment of course work of “Object
Oriented Programming” , 3rd Semester, Bachelor of Technology in Computer Science &
Engineering.
i
INDEX
CERTIFICATE i
INDEX ii
4.1 ALGORITHM
4.2 FLOWCHART
REFERENCES 20
ii
LIST OF FIGURES
iii
CHAPTER 1
PROBLEM STATEMENT
1
CHAPTER 2
INTRODUCTION
1. Inheritance:-
THEORY:
Inheritance, a cornerstone of object-oriented programming (OOP), facilitates code organization and
reuse by allowing a class to inherit attributes and behaviors from another class. At its core, a base
class serves as a blueprint, encapsulating common features shared among related classes. This
establishes an "is-a" relationship, where a derived class inherits from the base class, promoting a
hierarchical structure. In this way, the derived class gains access to the existing functionality of the
base class, promoting a modular and efficient design.
The process of inheritance enables developers to create specialized classes that build upon the
foundation laid by the base class. This customization can involve adding new attributes, modifying
existing behaviors, or overriding methods. The flexibility introduced by inheritance supports
polymorphism, where objects of the derived class can be treated as instances of the base class,
promoting adaptability and code flexibility.
While inheritance offers significant benefits, it is crucial to consider design implications, such as the
potential for the "diamond problem" in multiple inheritance scenarios. This challenge highlights the
importance of careful class hierarchy planning to avoid ambiguity and ensure a clear understanding
of relationships within the system. In summary, inheritance is a powerful mechanism that enhances
code organization, promotes code reuse, and fosters the development of scalable and maintainable
software systems in OOP.
Types Of Inheritance:-
Single Inheritance:
Single inheritance in Java occurs when a class extends only one superclass. It promotes the concept
of "is-a" relationship, where a subclass is a specific type of its superclass. For example, a Dog class
can extend an Animal class, indicating that a dog is a specific type of animal.
While Java doesn't support multiple inheritance of classes, it allows multiple inheritance through
interfaces. An interface defines a contract, and a class can implement multiple interfaces. This
enables a class to inherit method signatures from multiple sources, promoting flexibility in design.
Multilevel Inheritance:
Multilevel inheritance involves a chain of inheritance where a class extends another class, and then
another class extends the second class. This creates a hierarchical structure, and each subclass
inherits properties and behaviors not only from its immediate superclass but also from all the
superclasses up the hierarchy.
2
Hierarchical Inheritance:
In hierarchical inheritance, multiple classes inherit from a common superclass. Each subclass
represents a specialized version of the superclass. For example, an Animal class can be the
superclass, and Dog and Cat classes can be subclasses, inheriting common characteristics from the
Animal class.
Hybrid Inheritance:
Hybrid inheritance is a combination of different types of inheritance within a single program. It can
involve a mix of single, multiple, multilevel, or hierarchical inheritance. This approach allows
developers to leverage the advantages of different inheritance types based on the requirements of the
application.
3
2. Multithreading:-
Multithreading in Java :-
Multithreading is a programming concept where multiple threads (smaller units of a process) execute
independently but share the same resources. In Java, you can implement multithreading using the
Thread class or the Runnable interface. This project will use multithreading to concurrently analyze the
generated set of random numbers.
• Negative Numbers:
• A negative number is any number less than zero. In your program, you'll iterate through the
generated random numbers and count how many are negative.
Concurrency in Java:
• Concurrency is achieved in Java through the use of threads. Each type of number analysis
(negative, positive even, positive odd) can be performed concurrently by creating and running
separate threads for each task.
• The Thread class or the Runnable interface can be utilized to create these threads. This allows
the tasks to execute independently and concurrently.
Thread Safety:
• As multiple threads are accessing shared resources (the random numbers in this case), it's crucial
to ensure thread safety. Synchronization mechanisms such as synchronized blocks or using
thread-safe data structures should be considered to prevent data corruption.
By combining these concepts, your Java program will efficiently generate random numbers and
concurrently determine the count of negative, positive even, and positive odd numbers in the set,
showcasing the power of multithreading in parallel computation.
4
3. Constructor overloading in Java:-
In Java, constructor overloading is a powerful feature that allows a class to have multiple constructors
with different parameter lists. Constructors are special methods within a class that are invoked when an
object of that class is created. Constructor overloading provides flexibility in how objects are initialized,
allowing developers to create objects in various ways based on their needs.
Key Points:
Constructors in Java:
• Constructors are special methods with the same name as the class, and they are responsible for
initializing the state of an object when it is created.
• Unlike regular methods, constructors do not have a return type.
• When an object is created using the new keyword, a constructor is called to initialize the object.
Constructor Overloading:
• Constructor overloading occurs when a class has more than one constructor with different
parameter lists.
• The constructors in an overloaded set have the same name but differ in the number or types of
parameters they accept.
• This allows objects to be created and initialized in different ways, providing flexibility to the
developer.
Advantages of Constructor Overloading:
By leveraging constructor overloading, developers can create Java classes that are more versatile
and adaptable to different use cases, enhancing the overall design and usability of their code.
5
CHAPTER 3
DESIGN AND IMPLEMENTATION
3.1 ALGORITHM
6
Problem statement 2 (Algorithm):
Main Program:
• In the main program, generate a set of random numbers using the generateRandomNumbers
function.
• Specify the number of threads (numThreads) to be used for concurrent analysis.
• Split the array of random numbers into equal parts based on the number of threads.
• Create instances of the NumberAnalyzer class for each chunk.
• Create an array of threads to hold the instances of NumberAnalyzer.
• Start each thread, which will concurrently analyze its assigned chunk of numbers.
• Wait for all threads to complete using the join method.
• Collect and combine the results from each analyzer.
• Display the total count of negative, positive even, and positive odd numbers in the entire set
This algorithm outlines the steps to generate random numbers, create a concurrent analysis task using
multithreading, and then combine the results for further processing.
7
Problem statement 3 (Algorithm):
Define the Shape Class:
• Define a class named Shape.
• Include two overloading constructors in the Shape class.
• Constructor 1: Takes parameters to initialize properties related to the shape.
• Constructor 2: Default constructor with no parameters.
Main Method:
• In the main method:
• Instantiate objects of the Test1 class using both constructors.
• Call the calculate method on each object to calculate the volume.
8
3.2 FLOWCHART
10
Problem statement 3 (Flowchart):
11
3.3 SOURCE CODE
import java.util.Date;
class Person {
String name;
int age;
public Faculty(String name, int age, int ecNo, Date doj, String designation) {
super(name, age);
this.ecNo = ecNo;
this.doj = doj;
this.designation = designation;
}
System.out.println("Student Information:");
student.display();
System.out.println("\nStaff Information:");
staff.display();
System.out.println("\nFaculty Information:");
faculty.display();
}
}
13
Problem statement 2 (Source Code):
// Name : Sachin H. Rahangdale
//USN No. : CS23D017
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Override
public void run() {
switch (type) {
case "negative":
classifyNegative();
break;
case "even":
classifyEven();
break;
case "odd":
classifyOdd();
break;
}
}
14
private void classifyOdd() {
System.out.println("Positive Odd Numbers:");
for (int num : numbers) {
if (num > 0 && num % 2 != 0) {
System.out.print(num + " ");
}
}
System.out.println();
}
}
System.out.println("Generated Numbers:");
for (int num : numbers) {
System.out.print(num + " ");
}
System.out.println();
executorService.shutdown();
}
15
Problem statement 3 (Source Code):
// Name : Sachin Rahangdale
//USN No. : CS23D017
class Shape {
double length, width;
Shape(double side) {
this.length = side;
this.width = side;
}
double calculate() {
return length * width;
}
}
@Override
double calculate() {
return length * width * height;
}
}
16
CHAPTER 5
RESULT
Problem statement 1 (Result):
17
Problem statement 2 (Result):
18
Problem statement 3 (Result):
19
CHAPTER 6
CONCLUSION
The integration of multithreading into our project has brought forth the advantages of
concurrent execution. Through the use of threads, we have improved the efficiency of
our application by allowing multiple tasks to run concurrently. This not only enhances
performance but also provides a more responsive and dynamic user experience.
Constructor overloading has played a pivotal role in tailoring the initialization process of
our objects to meet diverse requirements. The ability to create multiple constructors with
varying parameter lists has granted flexibility and adaptability to our codebase, ensuring
that objects can be instantiated in different ways to suit specific needs.
As we reflect on the challenges and triumphs encountered during the development of this
microproject, it becomes evident that a solid understanding of these fundamental
concepts is crucial for building robust and scalable software solutions. The synergy
between inheritance, multithreading, and constructor overloading has empowered us to
create a well-structured and efficient application that can evolve to meet future demands.
In the ever-evolving landscape of software development, the knowledge gained from this
microproject serves as a foundation for tackling more complex challenges and exploring
advanced topics. The journey through inheritance, multithreading, and constructor
overloading has not only enhanced our coding skills but has also deepened our
appreciation for the elegance and power of object-oriented programming principles. As
we continue on our programming endeavors, these concepts will undoubtedly remain
essential tools in our toolkit for crafting high-quality and maintainable software
solutions.
20
REFERENCE
[1] www.w3school.com
[2] www.geekforgeeks.org
[3] Java Programming Book by TechMax
[4] www.tutorialspoint.com
[5] www.youtube.com
[6] www.code2flow.com
21