Define Java class, member, attribute, method, constructor and package

Today, we would like to present you some basic concepts from object oriented programming language – the Java class.

First, look around and try to describe what you see in the simplest way. You will find objects or persons that have their own characteristics and behaviours. They are interacting with you or you can interact with them. For instance, you are in your room, sitting on a chair and watching your cat sleeping on your desk.

In JAVA language, you would simply say that everything around you (the room, the char and the cat) is a different object. Those objects belong to specific groups: your cat is a member of the group of cats and your desk is a part of the furniture. Going further: every cat is a part of the group of felines, and felines are a sub-group of animals.
In JAVA, if you would like to describe not only your cat, but all other cats too, you would use rather something more general than an object – a class. A class provides a template for defining objects. It does not necessarily have to represent real object, it can be also a representation of a concept, process etc.

When we want to create a concrete cat, we will call a constructor – a special method inside the class used to initialize new objects. Every class has to have a constructor (if we do not define it, the compiler will use a default constructor).

For instance, the default constructor of a Cat class (representing all the sort of cats) is:


and this is how it is used to create a new Cat object:

Cat myCat = new Cat();

A class contains members: variables, functions or other classes (nested classes).

So our Cat can have a variable member catColour defining its colour:

String catColour;

And a member function that will implement its behaviour:

void purr();

Variable members can be also called attributes.
Usually, we regroup similar classes together into a package (a Cat could be inside an animals package containing also Dog class etc.), by adding a package declaration on the top of the class:

package animals;

Here is an example of the Cat class.

//the class is in the animals package
package animals;

public class Cat{
	//variable member or an attribute
	private String catColour;
	//instead of using the default constructor we
	//define a constructor taking one parameters: the _catColour
	Cat(String _catColour){
		catColour = _catColour;
	//a function member - implementing the cat behavior
	public void purr(){

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>