OBJECT AND CLASSES
Introduction to Object-Oriented Programming
Object-oriented programming (or OOP for short) is the dominant programming paradigm these days, having replaced the “structured,” procedure-based programming techniques that were developed in the early '70s. Java is totally object oriented, and it is impossible to program it in the procedural style that we may be most comfortable with.
We need to understand some of the terminology of OOP to go further. The most important term is the class, A class is the template or blueprint from which objects are actually made. This leads to the standard way of thinking about classes: as cookie cutters. Objects are the cookies themselves. When we construct an object from a class, we are said to have created an instance of the class. apply a method to an object, its state may change. manipulate the data, no other object will know or care. As we have seen, all code that we write in Java is inside a class.
Encapsulation (sometimes called data hiding) is a key concept in working with objects.
Formally, encapsulation is nothing more than combining data and behavior in one package and hiding the implementation of the data from the user of the object. The data in an object are called its instance fields, and the functions and procedures that operate on the data are called its methods. A specific object that is an instance of a class will have specific values for its instance fields. The set of those values is the current state of the object. Whenever we apply a method to an object, its state may change. When we extend an existing class, the new class has all the properties and methods of the class that we extend. We supply new methods and data fields that apply to our new class only. The concept of extending a class to obtain another class is called inheritance.
The syntax for creating a class is
class classname
{
variable declarations
method definitions
……
…}Here the class, is the keyword which should be prefixed in a class definition. All the data and methods are enclosed inside the class. We can place more than one class in a file, in that case only one class can have the main method and it should be public. This file should be saved as the name of the main method class. The following program consists of two classes and saved as a math.java ( main method class name ).
Program:
class methodclass
{
int x,y;
int add(int m, int n)
{
return m+n;
}
int sub(int m, int n)
{
return m-n;
}
int mul(int m, int n)
{
return m*n;
}
int div(int m, int n)
{
return m/n;
}
}
public class math
{
public static void main(String arg[])
{
if(arg.length==2)
{
methodclass m = new methodclass();
int a = Integer.parseInt(arg[0]);
int b = Integer.parseInt(arg[1]);
System.out.println("Arithmetic Operations\n");
System.out.println("The given numbers are "+a+", "+b);
System.out.println("Addition : "+m.add(a,b));
System.out.println("Subtraction : "+m.sub(a,b));
System.out.println("Multiplication : "+m.mul(a,b));
System.out.println("Division : "+m.div(a,b));
}
else
{
System.out.println("Invalid input, Usage : java math number1 number2");
}}}
Object-oriented programming (or OOP for short) is the dominant programming paradigm these days, having replaced the “structured,” procedure-based programming techniques that were developed in the early '70s. Java is totally object oriented, and it is impossible to program it in the procedural style that we may be most comfortable with.
We need to understand some of the terminology of OOP to go further. The most important term is the class, A class is the template or blueprint from which objects are actually made. This leads to the standard way of thinking about classes: as cookie cutters. Objects are the cookies themselves. When we construct an object from a class, we are said to have created an instance of the class. apply a method to an object, its state may change. manipulate the data, no other object will know or care. As we have seen, all code that we write in Java is inside a class.
Encapsulation (sometimes called data hiding) is a key concept in working with objects.
Formally, encapsulation is nothing more than combining data and behavior in one package and hiding the implementation of the data from the user of the object. The data in an object are called its instance fields, and the functions and procedures that operate on the data are called its methods. A specific object that is an instance of a class will have specific values for its instance fields. The set of those values is the current state of the object. Whenever we apply a method to an object, its state may change. When we extend an existing class, the new class has all the properties and methods of the class that we extend. We supply new methods and data fields that apply to our new class only. The concept of extending a class to obtain another class is called inheritance.
The syntax for creating a class is
class classname
{
variable declarations
method definitions
……
…}Here the class, is the keyword which should be prefixed in a class definition. All the data and methods are enclosed inside the class. We can place more than one class in a file, in that case only one class can have the main method and it should be public. This file should be saved as the name of the main method class. The following program consists of two classes and saved as a math.java ( main method class name ).
Program:
class methodclass
{
int x,y;
int add(int m, int n)
{
return m+n;
}
int sub(int m, int n)
{
return m-n;
}
int mul(int m, int n)
{
return m*n;
}
int div(int m, int n)
{
return m/n;
}
}
public class math
{
public static void main(String arg[])
{
if(arg.length==2)
{
methodclass m = new methodclass();
int a = Integer.parseInt(arg[0]);
int b = Integer.parseInt(arg[1]);
System.out.println("Arithmetic Operations\n");
System.out.println("The given numbers are "+a+", "+b);
System.out.println("Addition : "+m.add(a,b));
System.out.println("Subtraction : "+m.sub(a,b));
System.out.println("Multiplication : "+m.mul(a,b));
System.out.println("Division : "+m.div(a,b));
}
else
{
System.out.println("Invalid input, Usage : java math number1 number2");
}}}
output
c:\>javac math.java
c:\>java math 22 6
The given numbers are 22, 6
Addition :28
Subtraction :16
Multiplication :132
Division :3
C:\>java math
Invalid input, Usage : java math number1 number2
C:\>java math 44 5 7
Invalid input, Usage : java math number1 number2
Objects and Object VariablesTo work with objects, we first construct them and specify their initial state. Then we apply methods to the objects. In the Java programming language, we use constructors to construct new instances.
A Constructor is a special method whose purpose is to construct and initialize objects.
Constructor always have the same name as the class name. Thus, the constructor for the Date class is called Date . To construct a Date object, we combine the constructor with the new operator, as follows:
new Date()
While writing a constructor, keep the following in our mind
=> A constructor has the same name as the class.
=> A class can have more than one constructor.
=> A constructor may take zero, one, or more parameters.
=> A constructor has no return value.
=> A constructor is always called with the new operator.
Syntax for creating an object for a class
classname objectname = new classname();
For Example:
Date Birthday = new Date();
Through an object we can access any method of the particular class, For this we use a special
kind of operator called Dot operator ‘.’.
int d = Birthday.get(Calendar.DAY_OF_MONTH);
int m = Birthday.get(Calendar.MONTH);
c:\>java math 22 6
The given numbers are 22, 6
Addition :28
Subtraction :16
Multiplication :132
Division :3
C:\>java math
Invalid input, Usage : java math number1 number2
C:\>java math 44 5 7
Invalid input, Usage : java math number1 number2
Objects and Object VariablesTo work with objects, we first construct them and specify their initial state. Then we apply methods to the objects. In the Java programming language, we use constructors to construct new instances.
A Constructor is a special method whose purpose is to construct and initialize objects.
Constructor always have the same name as the class name. Thus, the constructor for the Date class is called Date . To construct a Date object, we combine the constructor with the new operator, as follows:
new Date()
While writing a constructor, keep the following in our mind
=> A constructor has the same name as the class.
=> A class can have more than one constructor.
=> A constructor may take zero, one, or more parameters.
=> A constructor has no return value.
=> A constructor is always called with the new operator.
Syntax for creating an object for a class
classname objectname = new classname();
For Example:
Date Birthday = new Date();
Through an object we can access any method of the particular class, For this we use a special
kind of operator called Dot operator ‘.’.
int d = Birthday.get(Calendar.DAY_OF_MONTH);
int m = Birthday.get(Calendar.MONTH);
No comments:
Post a Comment