0% found this document useful (0 votes)
5 views1 page

Java Multi-Threading and Inheritance Programs

The document outlines six Java programs demonstrating various programming concepts. These include displaying serial numbers using threads, implementing multiple inheritance, utilizing inheritance with overridden methods, calculating areas of different shapes through method overloading, demonstrating constructors, and performing matrix addition and multiplication. Each program includes specific input and expected output examples.

Uploaded by

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

Java Multi-Threading and Inheritance Programs

The document outlines six Java programs demonstrating various programming concepts. These include displaying serial numbers using threads, implementing multiple inheritance, utilizing inheritance with overridden methods, calculating areas of different shapes through method overloading, demonstrating constructors, and performing matrix addition and multiplication. Each program includes specific input and expected output examples.

Uploaded by

Ramesh Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Program - 1:

Java program for to display Serial Number from 1 to N by creating two Threads
Input: 10 Output: 1 2 3 4 5 6 7 8 9 10 Input: 4 Ouput: 1 2 3 4

Program - 2:
Java program on Multiple Inheritance Ex:MULTIPLE INHERITANCE USING
STUDENT MARK LIST Input: Sanjeevan//name of the student 1001//rollno
98//sub1 marks 99//sub2 marks 100//sub3 marks 60//practical marks, it should
be defined in interface

Program - 3:
Implement inheritance between Person (Aadhar, Surname, Name, DOB, and
Age) and Student (Admission Number, College, Course, Year)classes where
ReadData(), DisplayData() are overriding methods Input: 234//admission
number aditya//name of the college mpcs//course group 2022//joining date
0123456789//aadhar number U//surname Sanju//name of the student 30//age
09_05_1997//dob Output: Admission no:234 College Name:aditya Course
Name : mpcs Year of Joining:2023

Program - 4:
Calculate area of the following shapes using method overloading. a. Triangle
b. Rectangle c. Circle d. Square Input: 10.0 5.0 11.0 12.0 2.0 5.0 5.0 output:
Area of Triangle is 25.0 Area of Rectangle is 132.0 Area of Circle is 19.6 Area
of Square is 5.0

Program - 5:
Java program to demonstrate the use of Constructor. Ex: class Main { int
roll_no; String stu_name; Main() { //code here } Main(Parameters) //
Parameterized constructor { //code here } void display()
{ [Link](roll_no+" "+stu_name); } public static void main(String
args[]) { //read rno and string Main m1 = new Main(rno,string); [Link](); } }
Input: 1 Rajesh output: You called default Constructor 1 Rajesh

Program - 6:
Java program to implements Addition and Multiplication of two N X N matrices
Input: 2 1 2 3 4 1 0 0 1 Output: 2 2 3 5 1 2 3 4

Common questions

Powered by AI

Java does not support multiple inheritance directly to avoid complexity and errors such as the diamond problem. However, it can be achieved using interfaces. The student mark list program demonstrates multiple inheritance using interfaces, combining behavior from different sources without the risk of conflicting implementations from two parent classes. This approach allows for flexibility and modular design but can result in complex and hard-to-maintain code if not managed properly.

Overriding methods such as ReadData() and DisplayData() in additional classes allows for customized data input and output strategies, tailored to the specific requirements of different entities (like students in a database). It enhances data handling by allowing polymorphic behavior; different subclasses can handle input and display in unique ways based on their context. This ensures data operations are cohesive with the entity’s nature, improving both usability and utility of the system. For example, different formatting or data validation rules can be applied effortlessly through overriding.

Constructors in Java are special methods used to initialize objects. In the example program, both default and parameterized constructors are demonstrated. The default constructor initializes default values, while the parameterized constructor allows initializing instances with specified values (e.g., roll number and student name). Constructors must match the class name and have no return type, playing a critical role in object instantiation and setting up initial states.

Method overloading allows a class to have more than one method having the same name, as long as their parameter lists are different. In the program dealing with shapes, method overloading is used to allow for different implementations of an area calculation based on parameters (dimensions of shapes like triangle, rectangle, etc.). This technique significantly contributes to code clarity and maintainability in object-oriented programming by facilitating code reuse and improving readability through descriptive method naming.

Parameterized constructors improve Java object instantiation by allowing developers to create objects with pre-defined and meaningful states rather than relying on default values. This improves efficiency and clarity, as seen when student details like roll number and name are directly initiated for an object. They offer advantages over default constructors by reducing the need for separate setter methods to initialize objects post-instantiation, ensuring the object is fully ready for use immediately. They also enhance precision in setting initial conditions, avoiding potential errors from uninitialized fields.

Matrix operations like addition and multiplication are implemented using nested loops to iterate through each row and column. For addition, corresponding elements of two matrices are summed directly, whereas multiplication involves dot products of rows and columns from two matrices to compute each element of the result matrix. The challenge in these operations lies in ensuring efficient data handling, especially as the value of N increases, which can lead to significant computational complexity and time consumption. Handling such operations efficiently requires good understanding of algorithms and data structures.

Using multithreading allows the Java program to concurrently manage multiple threads of execution. In the program that displays serial numbers from 1 to N using two threads, it makes use of Java’s threading capabilities to execute code concurrently. This results in both threads working together to finish the task faster than if it were executed sequentially by a single thread. Each thread handles part of the number generation, potentially increasing efficiency and performance.

In the program demonstrating inheritance between the Person and Student classes, method overriding is used to implement polymorphism by defining a ReadData() and DisplayData() method in both classes. Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. When these methods are called on a Student object, the overridden methods are executed rather than those in the Person class, enabling different behaviors for these methods depending on the object's actual type. This is a foundation for polymorphism, providing flexibility and reuse of the code.

Java interfaces provide a way to achieve abstraction and multiple inheritances. By defining methods in interfaces, classes like the student mark list can implement multiple behaviors from various interfaces without being constrained by single inheritance. This approach enhances flexibility since a class can adopt multiple roles, as seen where a student entity can simultaneously implement interfaces for academic and extracurricular activities. Interfaces promote loose coupling, making the system more adaptable to changes such as adding new functionalities without modifying the existing classes.

Method overloading occurs when multiple methods with the same name but different parameter lists are defined within the same class (e.g., calculating area for different shapes). It allows methods to perform similar functions with different input types or numbers, enhancing readability and reusability. Conversely, method overriding involves defining a method in a subclass that already exists in the parent class, providing a specific implementation (e.g., overriding DisplayData() in Student class). Overriding enables polymorphism, allowing a subclass to modify or extend the behavior of its parent class methods.

You might also like