Java Homework Help | Help with Java Programming | Programming Homework Help | Java Programming Assignment Help | Help with Java Programming Assignment | Java Tutor | Programming Help | Java Online Help | Java Programming Online | Java Programming Projects

In Programming Homework Help Class is basic and essential part of any code program. Java students are essential to understand the concpet of class and its implementation in java programming. To help java students below is the concept description of class and its implimentation example to understand easily :-

Declaring a class
class in java declared with a keyword “class”

Syntax
class ClassName{
}

Example
class Person{
}

How to declare properties of a class
Each property is represented by a variable.

class Person{
float height, weight;
String color, country;
}

Since height and weight are real type values, so these are declared as a float. Where as color and country are of type String. So these are declared as type string.

Note:
We haven't discussed anything about strings, just note that string a group of characters.

How to define behaviors for a class
Each behaviour in Java represented by a method.

Method is a block of statements, with a name associated with it.
Syntax:
returnType methodName(parameters){

}
Example
int sum(int a, int b){
return (a+b);
}

here,
returnType is int
parameters are a, b.

return value must match with the return type of the method.
After adding the behaviors for the class Person, code changed like below
class Person{
float height, weight;
String color, country;

float getHeight(){
return height;
}

float getWeight(){
return weight;
}

String getColor(){
return color;
}

String getCountry(){
return country;
}
}

Creating Object
Syntax to create an Object
ClassName objectName = new ClassName();

Example
Person personA = new Person();

Here Person is the class name
personA is the object created for the class Person.

Once the class is defiined, you can create any number of objects to it.

Person personB = new Person();
Person personC = new Person();

How to access properties of an object
java provides '.' opertor to access th properties of an object.
Syntax:
objectName.propertyName = value;

Example
personA.height = 6;
personB.height = 5.5;

How to call the behaviors
Just like how you are accessing properties, you can call the behaviours.
Syntax
objectName.behavior();

Example
personA.getHeight();
personB.getHeight();

Complete Working Example of Person class
class Person{
float height, weight;
String color, country;

float getHeight(){
return height;
}

float getWeight(){
return weight;
}

String getColor(){
return color;
}

String getCountry(){
return country;
}

public static void main(String args[]){

/* Declaring two person objects */
Person personA = new Person();
Person personB = new Person();

/* Setting properties for personA object */
personA.height = 6;
personA.weight = 75;
personA.color = "White";
personA.country = "India";

/* Setting properties for personB object */
personB.height = 5.5f;
personB.weight = 76;
personB.color = "Black";
personB.country = "America";

/* Calling the behaviors and printing the details of personA */
System.out.println("PersonA Height is " + personA.getHeight());
System.out.println("PersonA Weight is " + personA.getWeight());
System.out.println("PersonA color is " + personA.getColor());
System.out.println("PersonA country is " + personA.getCountry());
System.out.println();

/* Calling the behaviors and printing the details of personB */
System.out.println("PersonB Height is " + personB.getHeight());
System.out.println("PersonB Weight is " + personB.getWeight());
System.out.println("PersonB color is " + personB.getColor());
System.out.println("PersonB country is " + personB.getCountry());
}
}

Output
PersonA Height is 6.0
PersonA Weight is 75.0
PersonA color is White
PersonA country is India

PersonB Height is 5.5
PersonB Weight is 76.0
PersonB color is Black
PersonB country is America

 

Our Team

We have 2500+ expert pool from all over the world. They are highly qualified and well experienced. All experts have experinced in teaching and good command over programming topics. Some of our experts are as follows:


John Doe

JAVA EXPERT(10+ Yrs Exp.)

Larry Doe

C EXPERT(12+ Yrs Exp.)

Ranith Kays

C++ EXPERT(8+ Yrs Exp.)

Joan Ray

OPEN SOURCE LANGUAGE EXPERT(8+ Yrs Exp.)