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.
Static variables?
Instance variables declares as static are essentially global variables.
- when object of class is created, copy of these static variables is not created.
- All instances of class share same static variables.
Static methods rules?
• They can only directly call other static methods of
their class.
• They can only directly access static variables of
their class.
• They cannot refer to this or super in any way. (The
keyword super relates to inheritance and is described in
the next chapter.)
Static block and use of static in class:
As soon as class is loaded, all static statements are run.
static BLOCK:
static{ initialise stuff; code; }
If you need to do computation in order to initialize your static variables, you can declare a static block that gets executed exactly once, when the class is first loaded.
Access static method, var outside class:
classname.method( )
specify the name of their class followed by the dot operator
final :
what does it do?
who can be declared as it?
A field can be declared as final. Doing so prevents its
contents from being modified, making it, essentially, a
constant. This means that you must initialize a final field
when it is declared. You can do this in one of two ways: First,
you can give it a value when it is declared. Second, you can
assign it a value within a constructor.
uppercase identifiers for final fields
method params, local vars can also be declared as firelds.
Declaring a parameter final
prevents it from being changed within the method. Declaring
a local variable final prevents it from being assigned a value
more than once.
final for methods is different.
Nested class:
Scope and lifespan and
It is possible to define a class within another class; such classes are known as nested classes.
The scope of a nested class is bounded by the scope of its enclosing class.
A nested class has access to the members, including private members, of the class in which it is nested. However, the enclosing class does not have access to the members of the nested class.
It is also possible to declare a nested class that is local to a block.
I dont understand
explain types of nested class:
Static and non static:
Static must access the nonstatic members of its enclosing class through an object. That is, it cannot refer to non-static members of its enclosing class directly.
Inner class:
An inner class is a non-static nested class. It has access to all of the variables and methods of its outer class and may refer to them directly in the same way that other non-static members of the outer class do.