Unit 3 part 2 java as OOP Flashcards
Ways of
Argument passing?
- Pass By value
2. Call by reference
Pass By value:
drawbacks too
copy of actual args passed to formal args.
Drawbacks:
In efficient storage allocation, copying arrays,objects is expensive.
When you pass a primitive type to a method, it is passed
by value.
Call by reference:
Formal param receive reference to actual data.
aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program.
-Note that when we pass a reference, a new reference variable is created. We cannot change the reference to refer to some other object, as the received reference is a copy of the original reference.
object as parameter:
One of the most common uses of object parameters
involves constructors. Frequently, you will want to construct
a new object so that it is initially the same as some existing
object.
overloading constructors.
method (classname objvar){}
Returning objects:
classname method ( ) {
return obj_of_classname;
}
worry of going out of scope?
Objects life span?
Since all objects are dynamically allocated using new, you
don’t need to worry about an object going out-of-scope
because the method in which it was created terminates. The
object will continue to exist as long as there is a reference to
it somewhere in your program. When there are no references
to it, the object will be reclaimed the next time garbage
collection takes place.
Features offered by encapsulation:
-encapsulation links data with the code that
manipulates it
-provides access control.
control what parts of a program can access the
members of a class
Black Box
Inner workings are not open for tampering
Access modifier:
name java’s access modifiers
Determines access, attached to declaration.
public, private, protected.
public:
When a
member of a class is modified by public, then that member
can be accessed by any other code.
Private:
then that member can only be
accessed by other members of its class.
main and its access modifier? why?
main( ) has always been preceded by the
public modifier. It is called by code that is outside the
program—that is, by the Java run-time system.
Default access modifier:
When no access modifier is used, then by default the member of a class is public within its own package, but cannot be accessed outside of its package
protected:
Comes in picture during inheritance
static is used for?
example?
Allows using a class member independently of any object.
- When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object
- methods and variables can be static
main( ) is declared as static because it must be
called before any objects exist.