If a problem is solved in terms of classes and objects. It is called OOP.
Class:-
Class:-
A class provides definition for an object. Normally variables of a class are declared as private members which are directly not accessible to the user and methods form public part of class which is accessible to user.
Object is an instance of a class.
Lets do an example:-
class Money
{
private int rs;
private int paisa;
public void set(int r, int p)
{
rs = r;
paisa = p;
}
public void show()
{
System.out.print(rs + " " + paisa);
}
}
Save it as Money.java
class UseMoney
{
public static void main(String args[ ])
{
Money m;
m1 = new Money();
m1.set(100,20);
System.out.print("First amount is");
m1.show();
Money m2 = new Money();
m2.set(200,30);
System.out.print("Second amount is");
m2.show();
}
}
Save it as UseMoney.java
Then compile
c:> javac Money.java
c:> javac UseMoney.java
Now you have compiled these two java files. Now you have to run the file which contains main function.
c:> java UseMoney
This will execute your java program.
Lets do an example:-
class Money
{
private int rs;
private int paisa;
public void set(int r, int p)
{
rs = r;
paisa = p;
}
public void show()
{
System.out.print(rs + " " + paisa);
}
}
Save it as Money.java
class UseMoney
{
public static void main(String args[ ])
{
Money m;
m1 = new Money();
m1.set(100,20);
System.out.print("First amount is");
m1.show();
Money m2 = new Money();
m2.set(200,30);
System.out.print("Second amount is");
m2.show();
}
}
Save it as UseMoney.java
Then compile
c:> javac Money.java
c:> javac UseMoney.java
Now you have compiled these two java files. Now you have to run the file which contains main function.
c:> java UseMoney
This will execute your java program.
No comments:
Post a Comment