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

Experiment 7

This document describes an experiment on defining classes, methods, objects, and method overloading in Java. It explains that a class is a logical entity that defines common properties, while an object is a physical entity with state, behavior, and identity. It provides examples of defining a class with fields and methods, creating objects, and initializing fields through methods. It also demonstrates method overloading by changing the number and data types of arguments. The conclusion states that the key concepts of classes, methods, objects, and method overloading in Java have been covered.

Uploaded by

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

Experiment 7

This document describes an experiment on defining classes, methods, objects, and method overloading in Java. It explains that a class is a logical entity that defines common properties, while an object is a physical entity with state, behavior, and identity. It provides examples of defining a class with fields and methods, creating objects, and initializing fields through methods. It also demonstrates method overloading by changing the number and data types of arguments. The conclusion states that the key concepts of classes, methods, objects, and method overloading in Java have been covered.

Uploaded by

69bunt Y
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

: 07

Title: Program to define class, methods and objects. Demonstrate method overloading.

Theory:

Objects and Classes in Java

In object-oriented programming technique, we design a program using objects and


classes. An object in Java is the physical as well as a logical entity, whereas, a class in Java is
a logical entity only.

An entity that has state and behavior is known as an object e.g., chair, bike, marker,
pen, table, car, etc. It can be physical or logical (tangible and intangible). The example of an
intangible object is the banking system.

Every object has 3 characteristics:

o State: represents the data (value) of an object.


o Behavior: represents the behavior (functionality) of an object such as deposit,
withdraw, etc.
o Identity: An object identity is typically implemented via a unique ID. The value of
the ID is not visible to the external user. However, it is used internally by the JVM to
identify each object uniquely.

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

Syntax to declare a class:

class <class_name>{ field;


method;
}

Instance variable in Java

A variable which is created inside the class but outside the method is known as an
instance variable. Instance variable doesn't get memory at compile time. It gets memory at
runtime when an object or instance is created. That is why it is known as an instance variable.

new keyword in Java

The new keyword is used to allocate memory at runtime. All objects get memory in
Heap memory area.
Object and Class Example: main within the class

Program 1: Java Program to illustrate how to define a class and fields.

//Defining a Student class.

class Student{

//defining fields
int id; //field or data member or instance variable
String name;

//creating main method inside the Student class


public static void main(String args[]){

//Creating an object or instance


Student s1=new Student(); //creating an object
Student

//Printing values of the object


System.out.println(s1.id); //accessing member through
reference variable
System.out.println(s1.name);
}
}

0
null
Output

Object and Class Example: main outside the class


Program 2: Java Program to demonstrate having the main method in another class

class Student{
int id;
0 String name;
null
}
//Creating another class TestStudent1 which contains the main method

class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}

Output

Program 3: Object and Class Example: Initialization through method

class Student{
int rollno;
String name;

void insertRecord(int r, String n){


rollno=r;
name=n;
}

void displayInformation(){
System.out.println(rollno+" "+name);}
}

class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}

111 Karan
222 Aryan
Output

Method Overloading in Java

If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading. If we have to perform only one operation, having same name
of the methods increases the readability of the program.

Advantage of method overloading

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

1. By changing number of arguments


2. By changing the data type

Method Overloading: changing no. of arguments

In this example, we have created two methods. The first add() method performs
addition of two numbers and second add method performs addition of three numbers.

Program 4:

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(11,11));
System.out.println(Adder.add(11,11,11));
}
}

Output

22
33

Method Overloading: changing data type of arguments


In this example, we have created two methods that differ in data type. The first add
method receives two integer arguments and second add method receives two double
arguments.

Program 5:

class Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}

class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}
}

Output

22
24.9

Conclusion: Hence we have studied how to define class, methods, object & method
overloading in java

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy