We already know about Object Oriented Programming and Packages, and also know how to use Scanner class form java.util package for getting inputs. And also have used System.out.println() method to print output. Java's API (Application Programming Interface) is a library of several packages, classes and interfaces etc. Let's explore more about classes and objects -
How to create own classes?
I have already explained in Object Oriented Programming, what is a class and what is an object. A class contains data and methods which act on the data. We have already seen how to create a class in Simple Program in Java. Class name can be any valid identifier. We have same restrictions on class names as the variables I mentioned in post Variable, Literals and Constants. CamelCase (first letter capital and other letters lowercase in each word used in name) method is used as a standard for naming classes.
Here is an example of a class Dog -
Here is an example of a class Dog -
Class Dog (Dog.java)
NOTE: In the class definition a keyword this is used to represents the reference of the current object (The object which invokes the method using the dot operator). As class variables and parameters have same name, using this keyword the program can differentiate between class variables and method parameters as both are accessible in the method.
So, we can take above program as template and can simplify the form of the class definition -
What are the methods?
A method consists the common code or functionality, to which the program can pass data through its parameters and can get some data in return. NOTE: When you pass a variable to a method, that is called an argument. But when a method receives a variable through calling, the variable is called parameter. The methods can be reused several times with different data. (there is no limit) You can pass any number of arguments (parameters) to a method from 0 too ..... any number. And but a method returns only one variable or no data. It can not return more than one variable. Suppose we have to get sum of two integer numbers -
Constructor methods are special methods. A constructor initializes an object when it is created. They have the same name as the class name. There is no need to specify return type for the constructor, as it always returns an instance of the class - an object. A class can have 0 or many constructors. When there is no constructor in the class definition, Java automatically provides a default constructor. Constructors also can be overloaded.
How to create an object?
To create an object from a class - you need to use the new operator with the constructor method. The new operator dynamically allocates (run time) memory for an object and returns a reference to it. To create an object, the general syntax is -
import java.awt.*; public class Dog { int age; int size; Color color; String colorName; Dog(int age, int size, Color color) { this.age = age; this.size = size; this.color = color; } public int getAge() { return this.age; } public void bark() { System.out.println("Bark - woof, woof"); } public Color getColor() { return this.color; } }In first line of the code, we have imported a package named java.awt, and we have used Color class from the package. We have created the class public, anyone can reuse this class. age, size, color and colorName - all variables are the data members of the class Dog. Dog(), getAge(), bark(), getColor() are method members of the class Dog.
NOTE: In the class definition a keyword this is used to represents the reference of the current object (The object which invokes the method using the dot operator). As class variables and parameters have same name, using this keyword the program can differentiate between class variables and method parameters as both are accessible in the method.
So, we can take above program as template and can simplify the form of the class definition -
import package; public class [ClassName] { type var1; type var2; type var3; type var4; ....... ClassName(parameters) { // body of method } type method1(parameters) { return var; } type method2(parameters) { return var; } type method3(parameters) { return var; } ..... }A class definition is just a prototype. And the variables and methods that constitute a class are called members of the class. You can see there is one method in above example which has same name as ClassName (Dog). That one is special method and is called as constructor method.
What are the methods?
A method consists the common code or functionality, to which the program can pass data through its parameters and can get some data in return. NOTE: When you pass a variable to a method, that is called an argument. But when a method receives a variable through calling, the variable is called parameter. The methods can be reused several times with different data. (there is no limit) You can pass any number of arguments (parameters) to a method from 0 too ..... any number. And but a method returns only one variable or no data. It can not return more than one variable. Suppose we have to get sum of two integer numbers -
public int sum(int a, int b) { int c = a + b; return c; }And if there are three numbers, you can define one more method with same name with three parameters. It is called method overloading. (NOTE: We will discuss overloading in more detail later.)
public int sum(int a, int b, int c) { int d = a + b + c; return d; }What are the Constructors?
Constructor methods are special methods. A constructor initializes an object when it is created. They have the same name as the class name. There is no need to specify return type for the constructor, as it always returns an instance of the class - an object. A class can have 0 or many constructors. When there is no constructor in the class definition, Java automatically provides a default constructor. Constructors also can be overloaded.
How to create an object?
To create an object from a class - you need to use the new operator with the constructor method. The new operator dynamically allocates (run time) memory for an object and returns a reference to it. To create an object, the general syntax is -
ClassName obj1 = new ClassName(parameter values);Here is the example of creating an object of the class Dog. The 3 parameters are passed as per the class definition given above (age, size and Color). Calling this constructor sets or initializes the values of 3 class variables age, size and color. -
Dog dog1 = new Dog(3, 4, Color.WHITE);
How to access class variables and methods?
To access class variables and methods the . (dot) operator is used. Like to access age and size of dog1, we should assign the class variable values to same data type as the variable is of.
int a = dog1.age; int b = dog1.size; System.out.println("Dog's age: " + a); System.out.println("Dog's size: " + b);
And to access methods of dog1, we should assign method to the variable of return data type. If there is none return data type (void) then you don't need to assign, just call the method using . (dot) operator.
a = dog1.getAge(); System.out.println("Dog's age: " + a); dog1.bark();
Whole program will look like this -
// Class definition Dog.java
import java.awt.*; public class Dog { int age; int size; Color color; String colorName; Dog(int age, int size, Color color) { this.age = age; this.size = size; this.color = color; } public int getAge() { return this.age; } public void bark() { System.out.println("Bark - woof, woof"); } public Color getColor() { return this.color; } }
/* To test class Dog - DogTest.java (entry point is main method as we have discussed before in our earlier posts */
public class DogTest { public static void main(String args[]) { Dog dog1 = new Dog(3, 4, Color.WHITE); int a = dog1.age; int b = dog1.size; System.out.println("Dog's age: " + a); System.out.println("Dog's size: " + b); a = dog1.getAge(); System.out.println("Dog's age: " + a); dog1.bark(); } }
No comments:
Post a Comment