What is JAVA Class

Classes in JAVA


A class is a prescription or a blueprint for a particular kind of the object. After declaring, a Class then it is used to create objects.
A class definition is very simple. There are just two kinds of things that you can include in a


class definition:
Fields—These are variables that store data items that typically differentiate one object of the
class from another. They are also referred to as data members of a class.

Methods—These define the operations you can perform for the class—so they determine what
you can do to, or with, objects of the class. Methods typically operate on the fields—the variables
of the class.


The fields in a class definition can be of any of the primitive types, or they can be references to objects of any class type, including the one that you are defining.

The methods in a class definition are named, self-contained blocks of code that typically operate on the fields that appear in the class definition.

General Syntax for the Class declaration is as below


class MyClass extends MySuperClass implements YourInterface {

// field, constructor, and
// method declarations

}


Class Decalarion

A class is defined with a keyword Class following

public class Horse {

}


And


public class Dog {

}


Above is the general syntax for a JAVA Class definition. Here is an example of JAVA Class declaration.


public class Horse {

String name;
int height;
String color;
double weight;

public String get_name(String newname){
name=newname;
return(name);

}

public int get_height(int newheight){

height=newheight;
return(height);

}

public String get_color(String newcolor){
color=newcolor;
return (color);
}

public double get_weight(double newweight){
weight = newweight;
return(weight);
}

/**
* @param args the command line arguments
*/

public static void main(String[] args) {

// TODO code application logic here

Horse myhorse =new Horse();

System.out.println("My Horse Name is "+myhorse.get_name("TEDAY HORSE"));
System.out.println("My Horse Color is "+myhorse.get_color("WHITE"));
System.out.println("My Horse Height is "+myhorse.get_height(10));
System.out.println("My Horse Weight is "+myhorse.get_weight(777.20));
}


}




The output of the above program is






//////////////////////////////////////////////////////////////////////////////////////

public class Dog

{
// Instance Variables
public String name; String breed; int age; String color;
// Constructor Declaration of Class
public Dog(String name, String breed,
int age, String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}
//Method
public String barking(){
return("Tuffy is Barking now..Booow...Booow...Booow");
}

public static void main(String[] args)
{
Dog tuffy = new Dog("tuffy","papillon", 5, "white");
System.out.println(tuffy.name);
System.out.println(tuffy.breed);
System.out.println(tuffy.age);
System.out.println(tuffy.color);
System.out.println(tuffy.barking());
}
}





In general, class declarations can include these components, in order:
Modifiers such as public, private, and a number of others that you will encounter later.
The class name, with the initial letter capitalized by convention.
The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
The class body, surrounded by braces, {}.