- Overloading Methods
- can have methods with the same method name with different parameter
- same name – diff params either in type or in numbers => polymorphism
- again we must also take into consideration java – autoboxing
- very helpful for knowing which methods to call – Maths.abs () is a single function for any kind of inputs -> while different methods need to be available in languages that do not support this feature
- it is the duty of the compiler to choose the correct method for a general action
- as a general practice, we use the overloaded methods for closely related operations
- Overloading constructors
- similar to overloading methods
- same name but different parameters help in initializing the same object using different values
- the correct matching overloaded constructor is called when “new” is executed
- Argument Passing
- in java all parameters are passed by value only and when objects are passed by value – it passes its reference which changes the values
- call-by-value
- copies the value of arguments into the formal parameter of the called method
- any changes made to the value of the parameter in the method has no changes to the original value
- all primitive types are passed by value
- call-by-reference
- only a reference of the value is passed to the argument
- any changes made to the params value in the method will change the original value as well
- all objects are passed by reference
- Returning Objects
- all objects reside in dynamically allocated “ new” operate
- any data type can be returned including objects, primitves
- Recursion
- recursion is a process of defining something in terms of itself
- allows a method to call itself
- and any method that calls itself is called recursive
- all params are allocated to a stack which rests until the base case and then pops back arguments
- can cause stack overrun
- there must be the base case with terminating logic which if not present will cause an infinite loop
- Access Control
- encapsulation provides an important attribute – access control to methods and variables in a class
- access modifiers are attached to the declaration which says how a member can be accessed from outside
- types
- private
- this member can be accessed only within that class
- protected
- only when inheritance is involved
- public
- any variable with such a modifier can be accessed by any other code
- default
- if none is given => it will be accessed only within the package
- private
- Static
- defines a class member which will be used independently of any object fo that class
- this can be used without any instance of the class
- any member declared static can be accessed before any objects of its class are created and without any reference to the class
- can declare methods and variables as static
- variables
- any instance variables declared as static are essentially global variables
- all the instances of the objects created from a class will keep a common copy of static variables
- methods
- only a static method can call another static method
- the can only directly access static data
- they cannot refer to this or super keywords
- block
- static blocks are declared if we need to do computation before the creation of the class
- this gets executed only once when class is loaded first
- To obtain a static variable from outside of the class we use – classname.method() / variable name
- Final
- any member variable/method can be final to prevent it from being modified
- final = final version = no changes = constant
- since the final variable cannot be modified it must be initialized when declared
- give value when declared
- give value thru constructor
- declaring a final variable means if cannot be changed within a method and also assigned with value more than once
- final references only will not change but what happens inside the object does not appear to be final
- So the conclusion is that the effectively final variable is a variable which behave like a final variable without declaring it as final.
- Nested and Inner Class
- Class within a class
- scope of a nested class is bounded by the scope of its enclosing class
- nested class has access to all members of the enclosing class and enclosing class does not have access to nested class members
- types
- static
- this has a static modifier to it
- since static, any access to its enclosing class members must be through an object
- non-static
- also called inner class
- non-static
- has access to all the members of the enclosing class
- instance of the inner class can be created only in the context of outer class
- static
- Varargs – variable length arguments
- a method that can take variable number of arguments – such method id varargs method
- (…) => tells compiler that this method can have more than one parameter
- this varargs are operated as array of specific type -> intact is an array which tells compiler that variable number of elements will be used in this
- can be overloaded
- using different type os var args
- using some more number of arguments
- can also be overloaded using normal methods