Friday, 22 January 2016
Java Classes Methods and Objects
Hello guys ,
In this post I am going to tell about what are classes , methods and objects in Java.
Classes, Methods and Objects are playing the important key role in creating a JAVA program. A programmer needs to know all about these concepts.
Let me tell the abstract meaning for Classes , methods and objects.
Consider the class as your house door’s locker ,
Consider the method as the things in your home.
and consider the objects as the key
to the locker of your door’s locker( i.e classes).
If you need to access the things (i.e methods) inside your home (i.e classes)you need to open the door of the house.
In order to open the door of the house we need a key to open the door.
That key is called as objects.
This is the basic concept happening in the coding part logically.
A class is defined as an entity that allows data and functions to work together, A class is considered as the user defined reference Data Type.
Predefined primitive Data Types are only limited and not used to represent real world things. But a class can represent real world entities through objects.
For example there is no Data Type to describe properties of a car, or a computer etc. A class Data Type only describes these things.
JAVA program is composed of full of classes, A JAVA program must have a main class containing main() method. Even the main() method also must be inside the class. So classes are more important. A class can have any number of objects.
JAVA allows accessing of one class’s method from another class, one of the main use of object creation is to initiate a class.
{....}
public method2()
public static void main(String args[])
For the easy for understanding consider the example
{ ...
}
public static void main(String args[])
{
}
As described above, a class can have several methods. In this example the
class home has method method fan.
For this reason different
methods are created based on the usage.
Member function provides the controlled access to the data members (Methods
are also called as member functions and Variables are also called as Data
Members).
Syntax:
Method_name()
{
//functions
In method name the closed bracket must present"( )"
public void fan()
import java.io.*;
class house
{
public void
fan()
{
System.out.println("method fan is accessed");
}
public
static void main(String args[])
{
}
In Object
Oriented Languages, programming and understanding the concept of large programs
is very easy.
Using objects we can access the member
variables (Variables of different methods) and member functions ( or methods)
of a class.
For Example:
Objects can be like flowers, rose, man or anything that used in the real
world.
Objects are the instantiations for class.
Object creation:
(OR)
class house // your house
If you need to access the things (i.e methods) inside your home (i.e classes)you need to open the door of the house.
In order to open the door of the house we need a key to open the door.
That key is called as objects.
This is the basic concept happening in the coding part logically.
Classes:
A class is defined as an entity that allows data and functions to work together, A class is considered as the user defined reference Data Type.
Predefined primitive Data Types are only limited and not used to represent real world things. But a class can represent real world entities through objects.
For example there is no Data Type to describe properties of a car, or a computer etc. A class Data Type only describes these things.
JAVA program is composed of full of classes, A JAVA program must have a main class containing main() method. Even the main() method also must be inside the class. So classes are more important. A class can have any number of objects.
JAVA allows accessing of one class’s method from another class, one of the main use of object creation is to initiate a class.
Syntax:
class class_name
{
public method1()
{....}
public method2()
{.....}
public static void main(String args[])
{....}
}
For the easy for understanding consider the example
class home
{
public fan()
{ ...
}
public static void main(String args[])
{
....
}
}
class home we already seen in the picture
As described above, a class can have several methods. In this example the
class home has method method fan.
Methods:
To perform various functions inside the class, various methods are created, if we enter various operations under single method, then that will increase the complexity level like Structural language(C Language).
For this reason different
methods are created based on the usage.
Member function provides the controlled access to the data members (Methods
are also called as member functions and Variables are also called as Data
Members).
Syntax:
Method_name()
{
//variables
//functions
}
In method name the closed bracket must present"( )"
In our example the method is
public void fan()
{
System.out.println(“method fan is accessed”);
}
The program after defining the method is given below
import java.io.*;
class house
{
public void
fan()
{
System.out.println("method fan is accessed");
}
public
static void main(String args[])
{
}
}
Now we are clear about what are classes and methods, let we learn how to access the methods inside the class.
The methods in the class is accessed from the main method public
static void main with the help of the objects ( key
).
OBJECTS:
The basic definition from all the books and references for object is "Object is a real-world entity" the reason is, objects can represent a name, or a place or any item that a program handles.
For example if the class is computer( class computer), monitor,
keyboard, hardware and software will be objects of computer class.
While considering Procedural Oriented Programming Languages understanding
the programming flow of the large program seems to be more difficult.
In Object
Oriented Languages, programming and understanding the concept of large programs
is very easy.
Object Oriented means, all the parts or components are in the form of objects and classes. So what is OBJECT( a main block in JAVA)Object is an instance of a class.
Using objects we can access the member
variables (Variables of different methods) and member functions ( or methods)
of a class.
For Example:
Objects can be like flowers, rose, man or anything that used in the real
world.
Objects are the instantiations for class.
Object creation:
class_name object_name=new class_name();
(OR)
class_name object_name;
object_name = new
class_name();
Example:
house h=new house();
(OR)
house h;
h = new house();
class_name object_name;
object_name = new
class_name();
house h=new house();
h = new house();
But exactly the meaning is object created from these above statements does not have any name, here h is the reference variable that holds the address of the object created for class house.
Let us see the full program in detail
import java.io.*;
class house // your house
public void fan() // fan inside your house{
{
System.out.println("method fan is accessed");
}
public static void main(String args[])
{
house h=new house();// object creation (i.e key for your house to access fan)
h.fan();// accessing the method fan by the object h
}
I hope now you are clear about the Java Classes, methods and objects.}
Also read,
How to run a Java Program
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment