In today's lesson we will learn how to describe our objects.

Objects require two things to perform their required tasks, attributes and behavior.

Attributes are the information stored within the object. They can be variables or they can be other objects such as the dog class in our example in lesson6a.

Behavior is the group of statements used to perform actions within the object. Each of these groups is called a method.

Creating Class Variables

The attributes of an object represent the variables that are needed for the object to function. An objects variables can be used throughout its program in any of the methods the object includes. You can create variables immediately after the class statement that creates the class and before any methods. The following code begins a class called dog with an integer variable called totDogs.

public class dog
public integer totDogs;

Only one copy of this variable exist for the whole class, all the variable we have created till now have been object variables. Class variables refer to a class of objects as a whole.

We assign and refer to class variable as we would object variables.

Creating Behavior with Methods

Behavior describes all the different sections of a class that accomplish specific tasks. Each of these sections is called a method.

All our programs to date have been using methods, for example println is a method to display text on a screen.

Declaring a method.

Creating a method is similar to creating a class, the difference is that methods can return a value after they are handled, if the method is not to return a value then we use a void statement.

boolean public healthyDog (String name)

boolean hasFleas = false;
// Statements to check if the dog has fleas
return true;

The healthyDog method is used to check if the dog has fleas. The method takes a single argument, the string variable called name, this is the name of the dog that will be checked for fleas.

When a method returns a value, you can use the method as part of an assignment statement. e.g..

if (doggy.hasFleas(dogName))
System.out.println("The dog " +
dogName +
" has fleas ");

Similar Methods with Different Arguments

Two methods can have the same name if they have a different number of arguments or the arguments are of different types. i.e.

void doggyName()
System.out.println("My dogs name is Barney");

void doggyName(String Name)

Constructor Methods

When you create an object in a program, we us the statement

dog doggy = new dog();

This statement creates a new dog object called doggy based on the class dog. We can assign the doggy variable when we create it by doing the following.

dog doggy = new dog("Barney");

Class Methods.

Like class variables, class methods are a way to provide functionality associated with an entire class instead of a specific object.

Variable Scope Within Methods.

When you create a variable or tips to develop a standing out music app or an object inside a method in one of you classes, it is useable only inside that method. You can only use a variable in more that one method if it was created as an object or class variable after the class statement of the beginning of the program.

Using the this Keyword

Because we can refer to variable and methods in other classes along with variables and methods in our own class, we can make things clearer with the this statement. The this statement is a way to refer in a program to its own object.

A variable called dogName exists within the scope of the checkFleas() method, but isn't the same variable as the object variable dogName. To refer to the current objects variable called dogName we could use the this statement as follows.

System.out.println("Dog Name " + this.dogName);

Views: 258


You need to be a member of Vanguard Online Community to add comments!

Join Vanguard Online Community

Forum Categories

© 2020   Created by Vanguard Media Ltd.   Powered by

Badges  |  Report an Issue  |  Terms of Service