- Objects
- Classes
- Inheritance
-
Programming in Java on Your Computer - Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Accessors and Mutators
Premium Content - Free Preview
You saw how to edit a method that returned the ID of a student. In Object-Oriented programming, you usually want to make an Object's variables private
which will prevent external code from directly accessing them. Instead, other code should use public methods of your Object for getting and modifying variable values. These methods are commonly called Accessors and Mutators.
Accessors
Accessor methods simply return the value of one of the Object's variables. For example, getStudentID()
returned the Student's ID, so it's an accessor. To get the ID of student1
in other code, you would type student1.getStudentID()
.
Question: Why couldn't I just make the student's ID (SID
) public, which would let me type student1.SID
to get the ID?
Answer: That would work but it would violate the principle of Encapsulation, and would be much less flexible. For example, let's say you want to always return additional information with the ID. With an accessor, you were able to modify the method easily so any code that accessed the ID will receive the extra information. (If you directly accessed the variables, you would need to edit your code everywhere the variable is accessed.)
You could also run additional checks in the accessor, such as checking that there is a Student ID in the first place.
Mutators
Mutators are methods that change the value of an Object's variables. They can do this by directly changing the value of a variable. For example, in a Car class, you could have a method to set the speed:
End of Free Content Preview. Please Sign in or Sign up to buy premium content.
Comments
calo
Jan 26, 12:43 PMyour missing input values here i guess
Learneroo
Jan 26, 12:45 PM@calo, in this challenge, there is no input.
calo
Jan 26, 12:50 PMArent we missing here an input for the options ?
calo
Jan 26, 2:33 PMyup my bad ..that was before I had the answer right
Lance
Feb 10, 8:23 PMthe input values are at the top:
Car c = new Car("green");
System.out.println( c.getColor() );
c.accelerate(5);
System.out.println( c.getSpeed());
jack
Mar 14, 9:34 AManybody know the answer. please post
jack
Mar 14, 9:44 AMplease post answers no comprendo
Learneroo
Mar 14, 9:47 AMJack, I will add a hint to help out. You only need to ask for help once! Also, please update your email info. Thanks.
jack
Mar 15, 11:02 AMoh thank you I had something capitalized that shouldn't have been
thales
Jul 8, 1:11 PMI have no idea what to do here? Please help me!
thales
Jul 8, 1:22 PMi figured it out, unbelievable i would never of guessed!
public void accelerate(int amount)
{
if (speed+amount<=0 || speed+amount>=140){
thales
Jul 8, 1:37 PMit is very hard to see the 'big picture'