Lesson 3: Object-Oriented Programming Flashcards
What are classes and objects?
A class is a template that defines the form of an object. It specifies both the data and the code that will operate on that data. Java uses a class specification to construct objects. Objects are instances of a class. Thus، a class is essentially Page 107a set of plans that specify how to build an object. It is important to be clear on one issue: a class is a logical abstraction. It is not until an object of that class has been created that a physical representation of that class exists in memory.
What are members/instance variables?
methods and variables that constitute a class are called members of the class. The data members are also referred to as instance variables.
What is the general form of a class definition?
A class is created by using the keyword class. A simplified general form of a class definition is shown here: class classname { // declare instance variables type var1; type var2; // … type varN; // declare methods type method1(parameters) { // body of method } type method2(parameters) { // body of method } // … type methodN(parameters) { // body of method } }
What does the dot operator do in Java?
The dot operator links the name of an object with the name of a member. The general form of the dot operator is shown here: object.member Thus، the object is specified on the left، and the member is put on the right. For example، to assign the fuelcap variable of minivan the value 16، use the following statement:
When you have multiple classes، which one should come first? The one with the main، or the one(s) without it?
Either class can be the first one in the file.
Are the instance variables in one object the same as the instance variables in another object?
each object has its own copies of the instance variables defined by its class. Thus، the contents of the variables in one object can differ from the contents of the variables in another. There is no connection between the two objects except for the fact that they are both objects of the same type.
How do you declare an instance that refers to an object?
This is done by using the new operator
What does the “new” operator do?
The new operator dynamically allocates (that is، allocates at run time) memory for an object and returns a reference to it. This reference is، essentially، the address in memory of the object allocated by new. This reference is then stored in a variable. Thus، in Java، all class objects must be dynamically allocated.
How many tasks does a method perform?
In well-written Java code، each method performs only one task.
What goes directly after a method’s name?
A method will have parentheses after its name
What is the general form of a method?
The general form of a method is shown here: ret-type name( parameter-list ) { // body of method } Here، ret-type specifies the type of data returned by the method. This can be any valid type، including class types that you create. If the method does not return a value، its return type must be void. The name of the method is specified by name. This can be any legal identifier other than those already used by other items within the current scope. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the method when it is called. If the method has no parameters، the parameter list will be empty.
Why does a method not need a dot operator when using an instance variables defined by it’s class?
When a method uses an instance variable that is defined by its class، it does so directly، without explicit reference to an object and without use of the dot operator. This is easy to understand if you think about it. A method is always invoked relative to some object of its class. Once this invocation has occurred، the object is known. Thus، within a method، there is no need to specify the object a second time.
What are the two forms of return statements?
There are two forms of return—one for use in Page 115void methods (those that do not return a value) and one for returning values.
What are the two ways in which a void method can return?
A void method can return in one of two ways—its closing curly brace is reached، or a return statement is executed.
What type of return returns a value?
Methods return a value to the calling routine using this form of return: return value; Here، value is the value returned. This form of return can be used only with methods that have a non-void return type. Furthermore، a non-void method must return a value by using this form of return.
What is a parameter?
Inside the method، the variable that receives the argument is called a parameter. Parameters are declared inside the parentheses that follow the method’s name.
How many parameters can a method have?
A method can have more than one parameter. Simply declare each parameter، separating one from the next with a comma. boolean methodName(int a، double b، float c)
What is a constructor and what do you use it for?
A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However، constructors have no explicit return type. Typically، you will use a constructor to give initial values to the instance variables defined by the class، or to perform any other startup procedures required to create a fully formed object.
Which classes have constructors?
All classes have constructors، whether you define one or not، because Java automatically provides a default constructor.
What happens when you set an object to the same value as a class? What if you set another object to the same value as the previous object?
This constructor assigns the instance variable x of MyClass the value 10. This constructor is called by new when an object is created.
How do you add parameters to a constructor?
Parameters are added to a constructor in the same way that they are added to a method: just declare them inside the parentheses after the constructor’s name. In this version of the program، the MyClass( ) constructor defines one parameter called i، which is used to initialize the instance variable، x.
What is the general form of the “new” operator?
the new operator has this general form: class-var = new class-name(arg-list); Here، class-var is a variable of the class type being created. The class-name is the name of the class that is being instantiated. The class name followed by a parenthesized argument list (which can be empty) specifies the constructor for the class. If a class does not define its own constructor، new will use the default constructor supplied by Java. Thus، new can be used to create an object of any class type. The new operator returns a reference to the newly created object، which (in this case) is assigned to class-var.
How does Java’s garbage system work?
Java’s garbage collection system reclaims objects automatically—occurring transparently، behind the scenes، without any programmer intervention. It works like this: When no references to an object exist، that object is assumed to be no longer needed، and the memory occupied by the object is released. This recycled memory can then be used for a subsequent allocation.
Why don’t I need to use new for variables of the primitive types، such as int or float?
Java’s primitive types are not implemented as objects. Rather، because of efficiency concerns، they are implemented as “normal” variables. A variable of a primitive type actually contains the value that you have given it. As explained، object variables are references to the object. This layer of indirection (and other object features) adds overhead to an object that is avoided by a primitive type.
When does garbage collection occur?
Garbage collection occurs only sporadically during the execution of your program. It will not occur simply because one or more objects exist that are no longer used. For efficiency، the garbage collector will usually run only when two conditions are met: - There are objects to recycle. - There is a reason to recycle them.
When is a “this” reference made?
When a method is called، it is automatically passed an implicit argument that is a reference to the invoking object (that is، the object on which the method is called). This reference is called this.
What is the most prominent use of the “this” keyword?
this has some important uses. For example، the Java syntax permits the name of a parameter or a local variable to be the same as the name of an instance variable. When this happens، the local name hides the instance variable. You can gain access to the hidden instance variable by referring to it through this.
What are the two basic types of class members and how do they effect the class code?
Although Java’s approach is a bit more sophisticated، in essence، there are two basic types of class members: public and private. A public member can be freely accessed by code defined outside of its class. A private member can be accessed only by other methods defined by its class. It is through the use of private members that access is controlled.
What type of security does restricting access to a class provide?
when correctly implemented، a class creates a “black box” that can be used، but the inner workings of which are not open to tampering.
How is member access control achieved?
Member access control is achieved through the use of three access modifiers: public، private، and protected. As explained، if no access modifier is used، the default access setting is assumed.
What does the private and public specifier mean? What happened when it’s default?
When a member of a class is modified by the public specifier، that member can be accessed by any other code in your program. This includes by methods defined inside other classes. When a member of a class is specified as private، that member can be accessed only by other members of its class. Thus، methods in other classes cannot access a private member of another class. The default access setting (in which no access modifier is used) is the same as public unless your program is broken down into packages.
Simply put، what are packages?
A package is، essentially، a grouping of classes. Packages are both an organizational and an access control feature،