Designing Classes

Designing Classes - Designing Classes 1 Classes...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Designing Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Classes – introduction A class is a blue print of an object. A class consists of some data (attributes) and methods. Encapsulation The variables contained in an object should be modified only within the object. Visibility modifiers/Access Specifiers They are used to declare classes, methods, and variables to define their accessibility . public – it can be accessed from anywhere (including outside of the class private – it can only be accessed from inside the class.
Background image of page 2
3 Accessor methods An accessor method can be defined to access private variable in the class since private data cannot be accessed from outside of the class. Mutator methods A mutator method can be defined to modify private variable in the class. public class Example1 { private int number1; //Accessor method for the variable “number1” public int getNumber1() { return number1; } //Mutator method for the variable “number1” public void setNumber1(int numberFromOutside) { number1 = numberFromOutside; } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 The toString methods -It is a method that takes no parameter and returns a String. -A returned string usually contains information on instance variables of its class. -Each class has a default toString method that contains its class object name and hash number. -When an object is used with System.out.print or println method, its toString method will be called.
Background image of page 4
5 public class Example { public static void main(String[] args) { Customer customer1 = new Customer(); System.out.println(customer1.toString()); // line1 System.out.println(customer1);
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/25/2010 for the course CSE 71682 taught by Professor Nakamura during the Spring '10 term at ASU.

Page1 / 13

Designing Classes - Designing Classes 1 Classes...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online