Java Second Internal Study Material
Java Second Internal Study Material
1. What is a class in Java and objects in java and how to create a class and its elements.
A class is a group of objects which have common properties. It is a template or blueprint from
which objects are created. It is a logical entity. It can't be physical.
o Fields
o Methods
o Constructors
o Blocks
o Nested class and interface
Example:
class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;
• class has multiple methods having same name but different in parameters, it is known
as Method Overloading
Example:
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(10,12));
System.out.println(Adder.add(10,20,30));
}
}
3.. Method Overriding in Java
• If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.Jav
• 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
met overriding.
class Bank{
int getRateOfInterest(){return
getRateOfInterest(){ 0;}
}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest(){return
getRateOfInterest(){ 8;}
}
import java.io.*;
// Driver class
class velu {
// Default Constructor
velu()
{
System.out.println("Default constructor");
}
// Driver function
public static void main(String[] args)
{
velu hello = new velu();
}
}
Output:
Default constructor
2. Parameterized Constructor in Java
A constructor that has parameters is known as parameterized constructor. If we want to initialize
fields of the class with our own values, then use a parameterized constructor.
import java.io.*;
class Mithran
{
// data members of the class.
String name;
int id;
Mithran(String name, int id)
{
this.name = name;
this.id = id;
}
}
class Harshan
{
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
Mithran s = new Mithran("velu", 35);
System.out.println("Name :" + s.name + " and Id :" + s.id);
}
}
Output:
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
There are two ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible but not
subpackages.
The import keyword is used to make the classes and interface of another package accessible to the
current package.
//save by B.java
package mypack;
import pack.*;
class B
{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
2) Using packagename.classname
If you import package.classname then only declared class of this package will be accessible.
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
3. Write a java program to multiply of two matrices (Lab Program 3)
ALGORITHM:
Step 1: Start the terminal and import necessary java packages.
Step 2: Create a class name as Matrix, then declare required data members
and matrices.
int m, n, p, q, sum = 0, c, d, k;
int first[][] = new int[m][n]; int second[][] = new int[p][q];
int multiply[][] = new int[c][d];
Step 3: Get row and column size of first matrix and second matrix.
Step 4: Check first matrix column size with second matrix row size
if(n!=p) is True means Multiplications is not possible.
Step 5: if(n!=p) is False means, Get first and second matrix values with help of
for loop.
Step 6: Then compute matrix multiplication
sum=sum+first[c][k]*second[k][d].
multiply[c][d]=sum
Step 7: Display the resultant matrix multiply[c][d] with matrix format.
Step 8: Stop the program.
PROGRAM:
import java.io.*;
import java.util.Scanner;
class Matrix
{
public static void main(String args[])throws IOException
{
int m, n, p, q, sum = 0, c, d, k;
Scanner ds=new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix");
m = ds.nextInt(); n = ds.nextInt();
int first[ ][ ] = new int[m][n];
System.out.println("Enter elements of first matrix");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
first[c][d] = ds.nextInt();
System.out.println("Enter the number of rows and columns of second matrix");
p = ds.nextInt(); q = ds.nextInt();
if (n != p)
System.out.println("The matrices can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("Enter elements of second matrix");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
second[c][d] = ds.nextInt();
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum; sum = 0;
}
}
System.out.println("Product of the matrices:");
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
}}}}