0% found this document useful (0 votes)
22 views34 pages

UNIE 2- NOTES.docx

Java notes unit 2 imp

Uploaded by

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

UNIE 2- NOTES.docx

Java notes unit 2 imp

Uploaded by

kumarshet16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Module 2

Introducing Classes: Class Fundamentals:

Java is an Object-Oriented programming language. In Java, the classes and objects are the basic
and important features of object-oriented programming system, Java supports the following

Fundamental OOPs concepts –

● Classes
● Objects
● Inheritance
● Polymorphism
● Encapsulation
● Abstraction
● Instance
● Method
● Message Passing

Java Classes

● A class is a blueprint from which individual objects are created (or, we can say a class is
a data type of an object type). In Java, everything is related to classes and objects. Each
class has its methods and attributes that can be accessed and manipulated through the
objects.
● For example, if you want to create a class for students. In that case, "Student" will be a
class, and student records (like student1, student2, etc) will be objects.
● We can also consider that class is a factory (user-defined blueprint) to produce objects.

Properties of Java Classes

● A class does not take any byte of memory.


● A class is just like a real-world entity, but it is not a real-world entity. It's a blueprint
where we specify the functionalities.
● A class contains mainly two things: Methods and Data Members.
● A class can also be a nested class.
● Classes follow all of the rules of OOPs such as inheritance, encapsulation, abstraction,
etc.
Types of Class Variables

● Local variables − Variables defined inside methods, constructors or blocks are called
local variables. The variable will be declared and initialized within the method and the
variable will be destroyed when the method has completed.
● Instance variables − Instance variables are variables within a class but outside any
method. These variables are initialized when the class is instantiated. Instance variables
can be accessed from inside any method, constructor or blocks of that particular class.
● Class variables − Class variables are variables declared within a class, outside any
method, with the static keyword.

Syntax to create a Java class

access_modifier class class_name{

data members;

constructors;

methods;

...;

Local Variable:
import java.io.*;

class DNT
{
public static void main(String[] args)
{
int var = 89; // Declared a Local Variable

// This variable is local to this main method only

System.out.println("Local Variable: " + var);


}
}

Instance Variable:
import java.io.*;
class DNT
{
public String student; // Declared Instance Variable
public DNT()
{ // Default Constructor
this.student= "Urmi Bose"; // initializing Instance Variable
}
//Main Method
public static void main(String[] args)
{
// Object Creation
DNT name = new DNT();
System.out.println("Student name is: " + name.student);
}
}

Static Variable:

import java.io.*;
class DNT
{
public static String student= "Urmi Bose"; //Declared static variable

public static void main (String[] args) {

//student variable can be accessed without object creation


System.out.println("Student Name is : "+DNT.student);
}
}

Example of a Java Class

In this example, we are creating a class "Dog". Where, the class attributes are breed, age,
and color.
The class methods are setBreed(), setAge(), setColor(), and printDetails().

// Creating a Java class


class Dog {
// Declaring and initializing the attributes
String breed;
int age;
String color;

// methods to set breed, age, and color of the dog


public void setBreed(String breed) {
this.breed = breed;
}
public void setAge(int age) {
this.age = age;
}
public void setColor(String color) {
this.color = color;
}

// method to print all three values


public void printDetails() {
System.out.println("Dog detials:");
System.out.println(this.breed);
System.out.println(this.age);
System.out.println(this.color);
}
}

Declaring Objects:

Java Objects

An object is a variable of the type class, it is a basic component of an object-oriented


programming system. A class has the methods and data members (attributes), these methods and
data members are accessed through an object. Thus, an object is an instance of a class.

If we consider the real world, we can find many objects around us, cars, dogs, humans, etc. All
these objects have a state and a behavior.

If we consider a dog, then its state is - name, breed, and color, and the behavior is - barking,
wagging the tail, and running.
Creating (Declaring) a Java Object

Object is created from a class. In Java, the new keyword is used to create new objects.

There are three steps when creating an object from a class −

● Declaration − A variable declaration with a variable name with an object type.


● Instantiation − The 'new' keyword is used to create the object.
● Initialization − The 'new' keyword is followed by a call to a constructor. This call
initializes the new object.
Syntax to Create a Java Object

Class_name object_name = new Class_name([parameters]);

Example to Create a Java Object

// Creating a Java class


class Dog {
// Declaring and initializing the attributes
String breed;
int age;
String color;

// methods to set breed, age, and color of the dog


public void setBreed(String breed) {
this.breed = breed;
}
public void setAge(int age) {
this.age = age;
}
public void setColor(String color) {
this.color = color;
}

// method to print all three values


public void printDetails() {
System.out.println("Dog detials:");
System.out.println(this.breed);
System.out.println(this.age);
System.out.println(this.color);
}
}

public class Main {


public static void main(String[] args) {
// Creating an object of the class Dog
Dog obj = new Dog();

// setting the attributes


obj.setBreed("Golden Retriever");
obj.setAge(2);
obj.setColor("Golden");

// Printing values
obj.printDetails();
}
}

Output
Dog detials:
Golden Retriever
2
Golden

Assigning Object Reference Variables:


classDemo {
intx = 10;
intdisplay()
{
System.out.println("x = "+ x);
return0;
}
}

classMain {
publicstaticvoidmain(String[] args)
{
Demo D1 = newDemo(); // point 1

System.out.println(D1); // point 2

System.out.println(D1.display()); // point 3
}
}

Output
Demo@214c265e
x = 10
0

importjava.io.*;
classDemo {

intx = 10;

intdisplay()

System.out.println("x = "+ x);

return0;

classMain {

publicstaticvoidmain(String[] args)

// create instance

Demo D1 = newDemo();

// accessing instance(object) variable

System.out.println(D1.x);

// point 3

// accessing instance(object) method


D1.display();

Output
10
x = 10

public class Main {


public static void main(String[] args) {
// Create two Integer objects
Integer num1 = new Integer(5);
Integer num2 = new Integer(10);

// Assign object reference variables


Integer sum = num1;
sum = add(sum, num2);

// Print the result


System.out.println("Sum: " + sum);
}

public static Integer add(Integer a, Integer b) {


return a + b;
}
}

Introducing Methods:

a set of instructions that can be called for execution using the method name.
// Java Program to Illustrate Methods

// Importing required classes


importjava.io.*;

// Class 1
// Helper class
classAddition{

// Initially taking sum as 0


// as we have not started computation
intsum=0;

// Method
// To add two numbers
publicintaddTwoInt(inta,intb)
{

// Adding two integer value


sum=a+b;

// Returning summation of two values


returnsum;
}
}

// Class 2
// Helper class
classGFG{

// Main driver method


publicstaticvoidmain(String[]args)
{

// Creating object of class 1 inside main() method


Additionadd=newAddition();

// Calling method of above class


// to add two integer
// using instance created
ints=add.addTwoInt(1,2);

// Printing the sum of two numbers


System.out.println("Sum of two integer values :"
+s);
}
}

Output
Sum of two integer values :3

Here is an example of Java code that introduces methods to add two numbers:

public class Main {


public static void main(String[] args) {
int num1 = 5;
int num2 = 10;

// Call the add method


int sum = add(num1, num2);

// Print the result


System.out.println("Sum: " + sum);
}

// Method to add two numbers


public static int add(int a, int b) {
return a + b;
}
}

Constructors:

Constructor in Java is used to create the instance of the class. Constructors are almost similar to
methods except two things- name is same as class name and has no return type.
A constructor in Java is a special method that is used to initialize objects.
The constructor is called when an object of a class is created. It can be used to set initial values
for object attributes.

Example:
// Create a Main class
publicclassMain{
int x;// Create a class attribute
// Create a class constructor for the Main class
publicMain(){
x =5;// Set the initial value for the class attribute x
}
publicstaticvoidmain(String[] args){
Main myObj =newMain();// Create an object of class Main (This will call the constructor)
System.out.println(myObj.x);// Print the value of x
}
}
// Outputs 5

A default constructor in Java is created automatically by the online Java compiler when the
programmer doesn't create any constructor in the entire program. It is created to assign the
default values to the instance variables of the class when an object is created.2
Example:

public class Calculator {


// Default constructor
public Calculator() {
}
// Method to subtract two numbers
public int subtract(int a, int b) {
return a - b;
}

public static void main(String[] args) {


// Create an instance of Calculator using the default constructor
Calculator calculator = new Calculator();

int num1 = 15;


int num2 = 7;

// Call the subtract method


int difference = calculator.subtract(num1, num2);

// Print the result


System.out.println("Difference: " + difference);
}
}

parameterized constructor:

In Java, a parameterized constructor is a constructor that takes one or more parameters and is
used to initialize objects with specific values.

public class Calculator {


private int num1;
private int num2;

// Parameterized constructor
public Calculator(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}

// Method to subtract two numbers


public int subtract() {
return num1 - num2;
}

public static void main(String[] args) {


// Create an instance of Calculator using the parameterized constructor
Calculator calculator = new Calculator(15, 7);

// Call the subtract method


int difference = calculator.subtract();
// Print the result
System.out.println("Difference: " + difference);
}
}

I
no-argument constructor

In Java, a no-argument constructor (also known as a no-arg constructor) is a constructor that


takes no parameters. It is used to create an object without passing any arguments.

public class Calculator {


private int num1;
private int num2;

// No-argument constructor
public Calculator() {
num1 = 0;
num2 = 0;
}

public void setNumbers(int num1, int num2) {


this.num1 = num1;
this.num2 = num2;
}

public int subtract() {


return num1 - num2;
}

public static void main(String[] args) {


Calculator calculator = new Calculator();
calculator.setNumbers(10, 5);
int result = calculator.subtract();
System.out.println("Result: " + result);
}
}

copy constructor :

A copy constructor in Java is a special type of constructor that creates a copy of an existing
object. It is a constructor that takes an object of the same class as a parameter and initializes the
new object with the same values as the existing object.

public class Person {


private String name;
private int age;

public Person(String name, int age) {


this.name = name;
this.age = age;
}

// Copy constructor
public Person(Person other) {
this.name = other.name;
this.age = other.age;
}
}

The this keyword :

The this keyword refers to the current object in a method or constructor. The most common
use of the this keyword is to eliminate the confusion between class attributes and parameters
with the same name (because a class attribute is shadowed by a method or constructor
parameter).

It helps distinguish between the class attributes and parameters with the same name.
For example, if you have a variable 'x', you can refer to it with 'this. x' inside your class methods.

Example: use program 3/Lab program as example.

Garbage Collection.

Garbage collection in Java is the process by which Java programs perform automatic memory
management. Java programs compile to bytecode that can be run on a Java Virtual Machine, or
JVM for short. When Java programs run on the JVM, objects are created on the heap, which is a
portion of memory dedicated to the program. Eventually, some objects will no longer be needed.
The garbage collector finds these unused objects and deletes them to free up memory.

The finalize() method is invoked each time before the object is garbage collected. This method
can be used to perform cleanup processing.
The gc() method is used to invoke the garbage collector to perform cleanup processing.

Example:

public class TestGarbage1{


public void finalize(){System.out.println("object is garbage collected");}
public static void main(String args[]){
TestGarbage1 s1=new TestGarbage1();
TestGarbage1 s2=new TestGarbage1();
s1=null;
s2=null;
System.gc();
}
}

Methods and Classes:

A method is a block of code or collection of statements or a set of code grouped together to


perform a certain task or operation. It is used to achieve the reusability of code. We write a
method once and use it many times. We do not require to write code again and again. It also
provides the easy modification and readability of code, just by adding or removing a chunk of
code. The method is executed only when we call or invoke it.

Example:

//user defined method


public static void findEvenOdd(int num)
{
//method body
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
Overloading Methods:

Method overloading in Java means having two or more methods (or functions) in a class with the
same name and different arguments (or parameters).

// Java program to demonstrate working of method


// overloading in Java
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}

// Overloaded sum(). This sum takes two double


// parameters
public double sum(double x, double y)
{
return (x + y);
}

// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}

Output
30
60
31.0

Objects as Parameters:

The Parameter Object design pattern is a way to group multiple parameters into a single object.

Similar to primitive types, Java makes it easier to give objects as parameters to methods. It is crucial to recognize
that sending an object as an argument transmits merely a reference to the item-not a duplicate of it. It means that any
changes made to the object inside the method will have an immediate impact on the original object. In this section,
we will discuss passing an object as a parameter in Java.

public class ObjectParameter{


private int attribute1;
private String attribute2;
private double attribute3;
// Constructor
public ObjectParameter(int attribute1, String attribute2, double attribute3) {
this.attribute1 = attribute1;
this.attribute2 = attribute2;
this.attribute3 = attribute3;
}
public void myMethod(ObjectParameter obj) {
System.out.println("Attribute 1: " + obj.attribute1);
System.out.println("Attribute 2: " + obj.attribute2);
System.out.println("Attribute 3: " + obj.attribute3);
}
public static void main(String[] args) {
ObjectParameter myObject1 = new ObjectParameter(10, "Hello", 3.14);
ObjectParameter myObject2 = new ObjectParameter(20, "World", 6.28);
myObject1.myMethod(myObject2);
}
}
Output:
Attribute 1: 20
Attribute 2: World
Attribute 3: 6.28

Argument Passing

There are different ways in which parameter data can be passed into and out of methods and
functions. Let us assume that a function B() is called from another function A(). In this case A is
called the “caller function” and B is called the “called function or callee function”. Also, the
arguments which A sends to B are called actual arguments and the parameters of B are
called formal arguments.

Important methods of Parameter Passing

1. Pass By Value: Changes made to formal parameter do not get transmitted back to the
caller. Any modifications to the formal parameter variable inside the called function or
method affect only the separate storage location and will not be reflected in the actual
parameter in the calling environment. This method is also called as call by value.
class CallByValue {

// Function to change the value


// of the parameters
public static void example(int x, int y)
{
x++;
y++;
}
}

// Caller
public class Main {
public static void main(String[] args)
{

int a = 10;
int b = 20;

// Instance of class is created


CallByValue object = new CallByValue();

System.out.println("Value of a: " + a
+ " & b: " + b);

// Passing variables in the class function


object.example(a, b);
// Displaying values after
// calling the function
System.out.println("Value of a: "
+ a + " & b: " + b);
}
}
Output:

Value of a: 10 & b: 20
Value of a: 10 & b: 20

2. Call by reference(aliasing):

Changes made to formal parameter do get transmitted back to the caller through
parameter passing. Any changes to the formal parameter are reflected in the actual
parameter in the calling environment as formal parameter receives a reference (or
pointer) to the actual data. This method is also called as call by reference. This method is
efficient in both time and space.

class CallByReference {
int a, b;

// Function to assign the value


// to the class variables
CallByReference(int x, int y)
{
a = x;
b = y;
}

// Changing the values of class variables


void ChangeValue(CallByReference obj)
{
obj.a += 10;
obj.b += 20;
}
}

// Caller
public class Main {

public static void main(String[] args)


{

// Instance of class is created


// and value is assigned using constructor
CallByReference object
= new CallByReference(10, 20);

System.out.println("Value of a: " + object.a


+ " & b: " + object.b);

// Changing values in class function


object.ChangeValue(object);

// Displaying values
// after calling the function
System.out.println("Value of a: " + object.a
+ " & b: " + object.b);
}
}

Output
Value of a: 10 & b: 20
Value of a: 20 & b: 40
Returning Objects:

Passing and Returning Objects


a parameter cannot be changed by the function, but the function can ask the parameter to change
itself via calling some method within it.
● While creating a variable of a class type, we only create a reference to an object. Thus, when
we pass this reference to a method, the parameter that receives it will refer to the same object
as that referred to by the argument.
● This effectively means that objects act as if they are passed to methods by use of
call-by-reference.
● Changes to the object inside the method do reflect the object used as an argument.
Illustration: Let us suppose three objects ‘ob1’ , ‘ob2’ and ‘ob3’ are created:

ObjectPassDemo ob1 = new ObjectPassDemo(100, 22);


ObjectPassDemo ob2 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob3 = new ObjectPassDemo(-1, -1);

From the method side, a reference of type Foo with a name a is declared and it’s initially
assigned to null.

boolean equalTo(ObjectPassDemo o);


As we call the method equalTo, the reference ‘o’ will be assigned to the object which is passed
as an argument, i.e. ‘o’ will refer to ‘ob2’ as the following statement execute.
System.out.println("ob1 == ob2: " + ob1.equalTo(ob2));

Now as we can see, equalTo method is called on ‘ob1’ , and ‘o’ is referring to ‘ob2’. Since
values of ‘a’ and ‘b’ are same for both the references, so if(condition) is true, so boolean true will
be return.
if(o.a == a && o.b == b)
Again ‘o’ will reassign to ‘ob3’ as the following statement execute.
System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));
● Now as we can see, the equalTo method is called on ‘ob1’ , and ‘o’ is referring to ‘ob3’. Since
values of ‘a’ and ‘b’ are not the same for both the references, so if(condition) is false, so else
block will execute, and false will be returned.

class ObjectPassDemo {
int a, b;

// Constructor
ObjectPassDemo(int i, int j)
{
a = i;
b = j;
}

// Method
boolean equalTo(ObjectPassDemo o)
{
// Returns true if o is equal to the invoking
// object notice an object is passed as an
// argument to method
return (o.a == a && o.b == b);
}
}

// Main class
public class GFG {
// MAin driver method
public static void main(String args[])
{
// Creating object of above class inside main()
ObjectPassDemo ob1 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob2 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob3 = new ObjectPassDemo(-1, -1);

// Checking whether object are equal as custom


// values
// above passed and printing corresponding boolean
// value
System.out.println("ob1 == ob2: "
+ ob1.equalTo(ob2));
System.out.println("ob1 == ob3: "
+ ob1.equalTo(ob3));
}
}

Output
ob1 == ob2: true
ob1 == ob3: false

Recursion:

Recursion is a process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called a recursive function.
Recursion provides a clean and simple way to write code.

Example 1: factorial of a number

int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}

output:
● 3= 3 *2*1 (6)
● 4= 4*3*2*1 (24)
● 5= 5*4*3*2*1 (120)

Example 2: Fibonacci series

public int fibonacci(int n) {


if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}

Access Control:

Access modifiers help to restrict the scope of a class, constructor, variable, method, or data
member. It provides security, accessibility, etc to the user depending upon the access modifier
used with the element
Private:

public class SumCalculator {


private int num1;
private int num2;

// Constructor
public SumCalculator(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}

// Private method to calculate sum


private int calculateSum() {
return num1 + num2;
}

// Public method to get sum


public int getSum() {
return calculateSum();
}

public static void main(String[] args) {


SumCalculator calculator = new SumCalculator(10, 20);
int sum = calculator.getSum();
System.out.println("Sum: " + sum);
}
}

Public:

public class SumCalculator {

// Public method to calculate sum


public static int calculateSum(int num1, int num2) {
return num1 + num2;
}

public static void main(String[] args) {


int num1 = 10;
int num2 = 20;
int sum = calculateSum(num1, num2);
System.out.println("Sum: " + sum);
}
}

Protected:

// Parent class with protected method


class Calculator {
protected int calculateSum(int num1, int num2) {
return num1 + num2;
}
}

// Child class inheriting from Calculator


public class SumCalculator extends Calculator {
public static void main(String[] args) {
SumCalculator calculator = new SumCalculator();
int num1 = 10;
int num2 = 20;
int sum = calculator.calculateSum(num1, num2);
System.out.println("Sum: " + sum);
}
}

1. Default Access Modifier


When no access modifier is specified for a class, method, or data member – It is said to be
having the default access modifier by default. The data members, classes, or methods that are
not declared using any access modifiers i.e. having default access modifiers are accessible only
within the same package.

class Blore
{
void display()
{
System.out.println("Hello World!");
}
}

2. Private Access Modifier


The private access modifier is specified using the keyword private. The methods or data
members declared as private are accessible only within the class in which they are declared.
● Any other class of the same package will not be able to access these members.
● Top-level classes or interfaces can not be declared as private because
o private means “only visible within the enclosing class”.
o protected means “only visible within the enclosing class and any subclasses”
Hence these modifiers in terms of application to classes, apply only to nested classes and not on
top-level classes

public class Employee {


// Private instance variables
private String name;
private int age;
private double salary;

// Public constructor
public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}

// Private method
private void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}

// Public method accessing private method


public void showDetails() {
displayDetails();
}
}

public class Main {


public static void main(String[] args) {
Employee emp = new Employee("John Doe", 30, 50000.0);
// Direct access to private members not allowed
// System.out.println(emp.name); // Error

// Accessing private members through public method


emp.showDetails();
}
}

3. Protected Access Modifier


The protected access modifier is specified using the keyword protected.
The methods or data members declared as protected are accessible within the same package or
subclasses in different packages.

public class A {
protected void display()
{
System.out.println("we are in VTU");
}
}
class B extends A {
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}

3. Public Access modifier


The public access modifier is specified using the keyword public.
● The public access modifier has the widest scope among all other access modifiers.
● Classes, methods, or data members that are declared as public are accessible from
everywhere in the program. There is no restriction on the scope of public data members.

public class A
{
public void display()
{
System.out.println("we are in VTU");
}
}
class B {
public static void main(String args[])
{
A obj = new A();
obj.display();
}
}

Understanding static:
The static keyword in Java is mainly used for memory management. The static keyword in Java
is used to share the same variable or method of a given class. The users can apply static
keywords with variables, methods, blocks, and nested classes.
The static keyword belongs to the class rather than an instance of the class. The static keyword is
used for a constant variable or a method that is the same for every instance of a class.
When you declare a variable or a method as static, it belongs to the class, rather than a specific
instance.

Example:
class Test
{
// static method
static void m1()
{
System.out.println("from m1");
}

public static void main(String[] args)


{
// calling m1 without creating
// any object of class Test
m1();
}
}
Output
from m1

Introducing final:

The final method in Java is used as a non-access modifier applicable only to a variable,
a method, or a class. It is used to restrict a user in Java.
The following are different contexts where the final is used:
Variable
Method
Class

Example:
public class ConstantExample {
public static void main(String[] args) {
// Define a constant variable PI
final double PI = 3.14159;

// Print the value of PI


System.out.println("Value of PI: " + PI);
}
}
Output
Value of PI: 3.14159

class GFG {

// a final variable
// direct initialize
final int THRESHOLD = 5;

// a blank final variable


final int CAPACITY;

// another blank final variable


final int MINIMUM;

// a final static variable PI


// direct initialize
static final double PI = 3.141592653589793;

// a blank final static variable


static final double EULERCONSTANT;

// instance initializer block for


// initializing CAPACITY
{
CAPACITY = 25;
}

Introducing Nested and Inner Classes:

it is possible to define a class within another class, such classes are known as nested classes.
They enable you to logically group classes that are only used in one place, thus this increases the
use of encapsulation and creates more readable and maintainable code.
// Outer class
public class Calculator {
// Inner class
public class Addition {
public int add(int num1, int num2) {
return num1 + num2;
}
}

// Main method
public static void main(String[] args) {
Calculator calculator = new Calculator();
Calculator.Addition addition = calculator.new Addition();
int num1 = 10;
int num2 = 20;
int sum = addition.add(num1, num2);
System.out.println("Sum: " + sum);
}
}

You might also like