0% found this document useful (0 votes)
9 views13 pages

Java Method and Constructor Overloading

The document contains multiple Java programs demonstrating various concepts such as method and constructor overloading, abstract classes, interfaces, inner classes, user-defined packages, exception handling with multiple catch blocks, producer-consumer problem using threads, multi-thread applications, and the ArrayList class. Each section includes code examples and expected outputs. The programs illustrate fundamental object-oriented programming principles in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

Java Method and Constructor Overloading

The document contains multiple Java programs demonstrating various concepts such as method and constructor overloading, abstract classes, interfaces, inner classes, user-defined packages, exception handling with multiple catch blocks, producer-consumer problem using threads, multi-thread applications, and the ArrayList class. Each section includes code examples and expected outputs. The programs illustrate fundamental object-oriented programming principles in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Write a java program for Method overloading and Constructor overloading

Method overloading:

import [Link].*;
class MethodOverloadingEx {

static int add(int a, int b)


{
return a + b;
}

static int add(int a, int b, int c)


{
return a + b + c;
}

public static void main(String args[])


{
[Link]("add() with 2 parameters");
[Link](add(4, 6));

[Link]("add() with 3 parameters");


[Link](add(4, 6, 7));
}
}

Output:
Constructor overloading
public class Student {
//instance variables of the class
int id;
String name;
Student(){
[Link]("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
public static void main(String[] args) {
//object creation
Student s = new Student(); [Link]("\
nDefault Constructor values: \n");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
[Link]("\nParameterized Constructor values: \n");
Student student = new Student(10, "Kalpana");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
}
}

Three test outputs:


Write a java program to represent Abstract class with example Program

abstract class Bank{


abstract int getRateOfInterest();
}
class SBI extends Bank
{
int getRateOfInterest()
{
return 7;
}
}
class PNB extends Bank
{
int getRateOfInterest(){return 8;}
}

class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
[Link]("Rate of Interest is: "+[Link]()+"
%"); b=new PNB();
[Link]("Rate of Interest is: "+[Link]()+" %");
}
}

Three Test Outputs:


Write a java program to implement Interface using extends keyword
class Person
{
String name;
Person(String n)
{
name = "Person: " + n;
}
}
interface Mother
{
public void FeedChildren();
}
interface Wife
{
public void CallHusband();
}

class WifeAndMother extends Person implements Wife, Mother


{
WifeAndMother(String n)
{
super(n);
name = "Wife and mother: " + n;
}
public void FeedChildren()
{
[Link](name + " is feeding the children.");
}
public void CallHusband()
{
[Link](name + " is calling her husband.");
}
}
class Test
{
public static void main(String args[])
{
Person p = new Person("SreeRam");
WifeAndMother w = new WifeAndMother("Seetha");
[Link]("p is a " + [Link] + " ");
[Link]("w is a " + [Link]);
[Link]();
[Link]();
}}
Three Test Outputs:

Output 1:
Write a java program to create inner classes
class A
{
int
a=10;
void
display(
)
{
B
b=new
B();
[Link](
);
}
class B
{
int
b=20;
void
show(
)
{
[Link](" a value is " +a);
[Link](" b value is "
+b);
}
}
}
class InnerDemo
{
public static void main(String args[])
{
A
a=new
A( );
[Link]
y( );
}
}

Three Test Outputs:


Write a java program to create user defined package

A. java
package
pack; public
class A
{
public void msg()
{
[Link]("Hello");
}
}

B. java
import pack.A;
class B
{
public static void main(String args[])
{
A obj = new A();
[Link]();
}
}

Three Test Outputs:


Write a java program for creating multiple catch blocks
public class MultipleCatchBlocks { public static void main(String[ ] args)
{
try
{
int a[ ]=new
int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
[Link]("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
[Link]("Parent Exception occurs");
}
[Link]("rest of the code");
}
}

Three Test Outputs:


Write a java program for producer and consumer problem using Threads
class InterThreadDemo
{
public static void main(String args[])
{
Producer p1=new Producer();
Consumer c1=new Consumer(p1);
Thread t1=new Thread(p1);
Thread t2=new Thread(c1);
[Link]();
[Link]();
}
}
class Producer extends Thread
{
StringBuffer sb;
Producer()
{
sb=new StringBuffer();
}
public void run()
{
synchronized(sb)
{
for(int i=0;i<=10;i++)
{
try
{
[Link](i+":");
[Link](1000);
[Link]("appending");
}
catch(InterruptedException e)
{
[Link](e);
}
}
[Link]();
}
}
}
class Consumer extends Thread
{
Producer prod;
Consumer(Producer prod)
{
[Link]=prod;
}
public void run()
{
synchronized([Link])
{
try
{
[Link]();
}
catch(Exception e)
{
[Link](e);
}
[Link]([Link]);
}
}
}
Three Test Outputs:
Write a Java program that implements a multi-thread application that has three
threads
class Thread1 extends Thread
{
public void run()
{
for(int i=0;i<=5;i++)
{
[Link]("Thread1:"+ i);
}
}
}
class Thread2 extends Thread
{
public void run()
{
for(int j=0;j<=5;j++)
{
[Link]("Thread2:"+ j);
}
}
}
class Thread3 extends Thread
{
public void run()
{
for(int k=0;k<=5;k++)
{
[Link]("Thread3:"+ k);
}}}
class MultiThreadDemo
{
public static void main(String args[])
{
Thread1 t1=new
Thread1(); Thread2
t2=new Thread2();
Thread3 t3=new
Thread3();[Link]();
[Link]();
[Link]();
for(int i=0;i<=5;i++)
{
[Link]("main thread:"+ i);
}
}}
Three Test Outputs:
Write a java program to represent ArrayList class
import [Link].*;
class TestJavaCollection
{
public static void main(String args[ ])
{
ArrayList<String> list=new ArrayList<String>(); //Creating arraylist
[Link]("Kalpana"); //Adding object in arraylist
[Link]("Venu");
[Link]("Suneetha");
[Link]("Gayatri");
//Traversing list through Iterator
Iterator itr=[Link]();
while([Link]())
{
[Link]([Link]());
}
}
}

Test output:

You might also like