Give declaration how to create class with explanation |EasyCoding45

Give declaration how to create class with explanation


In This tutorial we are going to learn how we can Create and declare a Class.

Definition :-

A Class Can be used to perform different operation using its object. A class includes Data members, functions().You can also specify the access for class , members, and functions() by specifying it to private, public, protected.


Note :-

  • If you don't specify the access specifier it will be private by default.

Syntax of Class :-

public class class_name
{
        public Data members;
         public void Function()
        {
            //Statements;
        }
}

Simple Example (Addition of Two numbers using class in java language ):-

Program :-

import java.util.*;

class Addition
{
int sum;

public void Add(int n1,int n2)
{
sum=n1+n2;

System.out.println("Addition is :"+sum);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter Two Numbers :");

int n1=sc.nextInt();

int n2=sc.nextInt();

Addition ad=new Addition();

ad.Add(n1,n2);
}
}

Output :-




Comments