


class class_name
{
// some data
// some functions
};
class temp
{
private:
int data1;
float data2;
public:
void func1()
{ data1=2; }
float func2() {
data2=3.5;
return data;
}
};
Explanation
C#
public class Customer
{
//Fields, properties, methods and events go here...
}
using System;
class Example
{
public Example(int property)
{
Property = property;
}
public int Property { get; private set; }
}
Another example:
class Program
{
static void Main()
{
Example example = new Example(5);
Console.WriteLine(example.Property);
}
}
C#
class ObjectTest
{
public int i = 10;
}
class MainClass2
{
static void Main()
{
object a;
a = 1; // an example of boxing
Console.WriteLine(a);
Console.WriteLine(a.GetType());
Console.WriteLine(a.ToString());
a = new ObjectTest();
ObjectTest classRef;
classRef = (ObjectTest)a;
Console.WriteLine(classRef.i);
}
}
/* Output
1
System.Int32
1
* 10
*/
Local variables
Class RegistrationForm
{
Int studentNumber;
Int courseNumber;
};
A class definition has three parts:
-Keyword class
-Class name
-Class body
Example of Class in JAVA
class myMath
{
//Methods
public int Add(int i, int j)
{
return i + j;
}
}
JAVA Object
The Object Class is the super class for all classes in Java. Some of the object class methods are:
equals
toString()
wait()
notify()
notifyAll()
hashcode()
clone()
An object is an instance of a class created using a new operator. The new operator returns a reference to a new
instance of a class. This reference can be assigned to a reference variable of the class. The process of creating
objects from a class is called instantiation. An object encapsulates state and behavior. An object reference
provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via
references, which can be stored in variables.
Example of Defined Object in JAVA
public class Cube
{
int length = 10;
int breadth = 10;
int height = 10;
public int getVolume()
{
return (length * breadth * height);
}
public static void main(String[] args) {
Cube cubeObj; // Creates a Cube Reference
cubeObj = new Cube(); // Creates an Object of Cube
System.out.println("Volume of Cube is : " + cubeObj.getVolume());
}
}