Chapter 3 - Classes some basics Flashcards
what is a class in terms of type
- A class is a special kind of programmer-defined type, and variables can be declared of a class type - A value of a class type is called an object or an instance of the class
primitive vs class type values
- prim -sinlge piece of data
- classes - multiple pieces of data as well as methods
- All instances of a class have the same methods
– All instances of a class have the same pieces of data (i.e.,
name, type, and number)
– For a given instances, each piece of data can hold a
different value
what are the contents of class definition?
- specifies that data items and the methods that all of its objects will have
- data items - fields - instance variables
how to invoke a method
use the calling object and the mthod name
classVar.mymethod();
two method categories
- perform action - dont return, void
- compute and return a value
keywords used for methods
typeReturned methodname
void
main
turning a method into a void method
objectName.returnedValueMethod()
local variable
variable declared within a method definition
examples of local variables
- declared in the main method
- method parameters
- If two methods each have a local variable of
the same name, they are still two entirely
different variables - for loop
formal parameters
some methods need additional data via a list of parameters in order to perform their work
what does a parameter list do
A parameter list provides a description of
the data required by a method
– It indicates the number and types of data
pieces needed, the order in which they must be
given, and the local name for these pieces as
used in the method
what are arguments
actual parameters
when a method is invoked, the appropriate values must be passed in the form of arguments
number and order must match that of the para list
type of each argument must be compatible with that of the corresponding parameter
call by value mechanism
Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter.
pass by reference vs pass by value
Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that’s why it’s called pass by value.
Pass by Reference: An alias or reference to the actual parameter is passed to the method, that’s why it’s called pass by reference.
what happens if arguments and parameters dont match
java will try an automatic type conversion
what is a constructor?
A constructor is a special kind of method that
is designed to initialize the instance variables
for an object:
the this keyword in constructors
this refers to the instance variables e..g this.word (instance variable word) = word
what is overloading?
when two or methods in the same class have the same name - to be valued they must have different signatures(i.e. their parameter list must differ) - they cant have different return types
what are the default variable declarations
boolean - false class type - null string - ""
accessor and mutator methods
public String getString(){return String;}
public void setName(){this.name = name;}
describe an encapsulated class
implemented details hidden in the capsule
-private instance variables
-private constants
-private methods and bodies f public and private method definitions
interface available to a programmer using the class :
- comments
-headers of methods
-public defined constants
a class definition should have no public instance variables
public vs private modifiers
public - no restrictions on where an instance variable or methods can be used
private - instance variable or method cannot be accessed by name outside the class, private methods are typically helper methods
each class typically needs to have its own….methods
toString and equals
example of a toString method
public String toString(){return “Name:”+”name”}