Programming in Java
Object Oriented in Java
Advanced topics in Java
Tutorials
C is a very powerful and widely used language. It is used in many scientific programming situations. It forms (or is the basis for) the core of the modern languages Java and C++. It allows you access to the bare bones of your computer.C++ is used for operating systems, games, embedded software, autonomous cars and medical technology, as well as many other applications. Don't forget to visit this section...
Class and Object
As we know java is an object oriented language so everything is surrounded class and object.
Class may defined in many ways like “Collection of similar types of elements” , “User Defined Data types” or “Template for variables and functions” etc.
Object is the real entity and may be defined as the “instance of class”.
In Java we can create the class as following :
class MyClass
{
int x=100;
void display()
{
System.out.println("x="+x);
}
}
⇒ The above class has one class variable and one method but it does not have the main method, so directly it cannot be run.
⇒ This class may be run by creating the object of this class into another class.
Object of the class can be created as following :
MyClass obj= new MyClass();
// Here we are creating obj (object) of MyClass (class) .
⇒ When we call the function we may also pass the parameters and the function may return the value.
class Area { int length=0; int breadth=0; //class variables void setData(int l, int b) { length=l; //class variable initialized here by local variable breadth=b; } int areaCal() { int area=length*breadth; return area; } } class checkMain { public static void main(String args[]) { Area a= new Area(); // object is created here int l=20, b=10; // these values may be taken as input from user we will take that later. a.setData(l,b); int ar=a.areaCal(); //the areaCal Function return the value here. System.out.println("area is"+ar); } }
output: area is 200.