0% found this document useful (0 votes)
2 views

Java Second Internal Study Material

The document provides an overview of Java concepts including classes, objects, method overloading, method overriding, constructors, and packages. It explains how to declare classes, create objects, and implement method overloading and overriding with examples. Additionally, it discusses the types of constructors and provides a Java program for multiplying two matrices.

Uploaded by

john8012karthik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Second Internal Study Material

The document provides an overview of Java concepts including classes, objects, method overloading, method overriding, constructors, and packages. It explains how to declare classes, create objects, and implement method overloading and overriding with examples. Additionally, it discusses the types of constructors and provides a Java program for multiplying two matrices.

Uploaded by

john8012karthik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

5 Marks

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.

A class in Java can contain:

o Fields
o Methods
o Constructors
o Blocks
o Nested class and interface

Class Declaration in Java


access_modifier class <class_name>
{
data member;
method;
constructor;
nested class;
interface;
}

Example:

class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;

public static void main(String args[])


{
// creating an object of
// Student
Student s1 = new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
2. Method Overloading in java

• class has multiple methods having same name but different in parameters, it is known
as Method Overloading

There are two ways to overload the method in java

• By changing number of arguments


• By changing the data type

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.

• Method overriding is used to provide the specific implementation of a method which is


already provided by its super class.
• Method overriding is used for runtime polymorphism

class Bank{
int getRateOfInterest(){return
getRateOfInterest(){ 0;}
}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest(){return
getRateOfInterest(){ 8;}
}

class ICICI extends Bank{


int getRateOfInterest(){return
getRateOfInterest(){ 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return
getRateOfInterest(){ 9;}
}
//Test class to create objects and call the methods
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI
"SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI
"ICICI Rate of Interest: "+i.getRateOfInterest());
+i.getRateOfInterest());
System.out.println("AXIS
"AXIS Rate of Interest: "+a.getRateOfInterest());
+a.getRateOfInterest());
}
}
10- Marks
1. Constructors in Java
• a Constructor is a block of codes similar to the method. It is called when an instance of
the class is created.
• At the time of calling the constructor, memory for the object is allocated in the memory.
• It is a special type of method that is used to initialize the object. Every time an object is
created using the new() keyword, at least one constructor is called.

Rules for creating Java constructor

There are two rules defined for the constructor.

1. Constructor name must be the same as its class name


2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, final, and synchronized

Types of Java constructors

There are two types of constructors in Java:

1. Default constructor (no-arg constructor)


2. Parameterized constructor

1. Default constructor (no-arg constructor):

A constructor that has no parameters is known as default the constructor.

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:

Name :velu and Id :35


2. Explain Java Package
A java package is a group of similar types of classes, interfaces and sub-packages.

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.

Advantage of Java Package

1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

How to access package from another package?

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.

Example of package that import the packagename.*


/save by A.java
package pack;
public class A
{
public void msg(){System.out.println("Hello");}
}

//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.

Example of package by import package.classname

//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");
}}}}

You might also like