Core Flashcards
Access Modifiers
*used for setting the access level to classes, variables, methods, and constructors.
1.public –>available to all other classes in all packages
2.protected –> we can access the member from the same package and in addition from all subclasses of its class, even if they lie in other packages
3.default (no keyword)–>package-private, which means that all members are visible within the same package but aren’t accessible from other packages
4.private –>accessible from the same class only
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N
The most restrictive:?????
private<default<protected<public
Interfaces
ARE BEING IMPLEMENTED!!
Fields defined in an interface are ALWAYS considered as public, static, and final. Even if you don’t explicitly define them as such. In fact, you cannot even declare a field to be private or protected in an interface. Therefore, you cannot assign any value to ‘type’ outside the interface definition–> the program will not COMPILE
Clases
ARE BEING EXTENDED!
Access to static and instance fields and methods
Access to static and instance fields and static methods depends on –> reference
Access to instance methods depends on –> the class of the object
Access to static and instance fields and static methods depends on the class of reference variable and not the actual object to which the variable points to.
Observe that this is opposite of what happens in the case of instance methods. In case of instance methods the method of the actual class of the object is called.
https://www.youtube.com/watch?v=5kY35dkMOqY
*instance variables can be accessed only from a non-static blocks.
*static variables can be accessed from any block.
Ordinea operatilor
From left to right: the variables are being evaluated, then the operands and afterwards the methods
EG:
String str1 = “one”;
String str2 = “two”;
System.out.println( str1.equals(str1=str2) );
First the value of ‘str1’ is evaluated (i.e. one). Now, before the method is called, the operands are evaluated, so str1 becomes “two”. so “one”.equals(“two”) is false.
* the embedded assignments occur before the method calls
getArray()[index=2]++;
*Note that boolean operators have more precedence than =. (In fact, = has least precedence of all operators.)
Imports
- Package import does not use static keyword!–> WRONG: import static packge.*;
If the package is imported then the static field can be accessed as Class.staticField:–> import package.;
*Syntax for importing static fields is: import static <package>.<classname>.*; or import static <package>.<classname>.<fieldname>;--> can use the STATIC FIELD stand ALONE</fieldname></classname></package></classname></package>
Object methods
*getClass is a public instance method in Object class. That means it is polymorphic. In other words, this method is bound at run time and so it returns the NAME of the class of the actual OBEJCT to which the reference points.
All wrapper classes are final and so they cannot be extended.
Wrapper classes:
java.lang.Boolean, java.lang.Integer, java.lang.Long, java.lang.Short java.lang.String, StringBuilder, and StringBuffer,java.lang.System
*java.lang.Number, however, is not final. Integer, Long, Double etc. extend Number!!!
JAVA OVERWRITING rules
***Overriding refers to the ability of a subclass to re-implement an instance method inherited from a superclass.
DO NOT CONFUSE with OVERLOADING
Rules from overriding:
#1:Only inherited methods can be overridden.
#2:Final and static methods cannot be overridden.
#3: The overriding method must have same argument list.
#4: The overriding method must have same return type (or subtype).
#5: The overriding method must not have more restrictive access modifier.
#6: The overriding method must not throw new or broader checked exceptions.
#7:Use the super keyword to invoke the overridden method from a subclass.
#8:Constructors cannot be overridden.
#9: Abstract methods must be overridden by the first concrete (non-abstract) subclass.
#10: A static method in a subclass may hide another static one in a superclass, and that’s called hiding.
#11: The synchronized modifier has no effect on the rules of overriding.
#12: The strictfp modifier has no effect on the rules of overriding.
***Overloading
#1. Method overloading can only be achieved if and only if two or more methods share the same method name but have different method signatures. If more than one method has the same name as well as signature within the same class, the program would not compile successfully.
#2. Return type is NOT relevant for overloading.
JAVA OVERWRITING rules
switch
Works with the:
*String, char si Character
*byte, short, int and their wrapper classes Byte, Short, and Integer
*enumerated types (discussed in Enum Types),
Does NOT work with BOOLEAN!!
WRAPPER CLASSES
Methods:
Input String–> output Primitive!!!!!!!
parseXXX(String s)
Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”.
new Boolean(null) will return a Boolean wrapper object containing false!!!!!!!
**
new Boolean() == false
This will not compile because Boolean class does not have a no-args constructor.
**
new Boolean(“TrUe”) == new Boolean(true)
Even though both the sides have a Boolean wrapper containing true, the expression will yield false because they point to two different Boolean wrapper objects
new Boolean(“no”) == false
Any string other than “true” (ignoring case) will produce a Boolean containing false
- Boolean class has two constructors - Boolean(String) and Boolean(boolean)
The String constructor allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string “true”. Otherwise, allocate a Boolean object representing the value false. Examples: new Boolean(“True”) produces a Boolean object that represents true. new Boolean(“yes”) produces a Boolean object that represents false.
The boolean constructor is self explanatory.
- Boolean class has two static helper methods for creating booleans - parseBoolean and valueOf.
Boolean.parseBoolean(String ) method returns a primitive boolean and not a Boolean object (Note - Same is with the case with other parseXXX methods such as Integer.parseInt - they return primitives and not objects). The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”.
Boolean.valueOf(String ) and its overloaded Boolean.valueOf(boolean ) version, on the other hand, work similarly but return a reference to either Boolean.TRUE or Boolean.FALSE wrapper objects. Observe that they dont create a new Boolean object but just return the static constants TRUE or FALSE defined in Boolean class.
- When you use the equality operator ( == ) with booleans, if exactly one of the operands is a Boolean wrapper, it is first unboxed into a boolean primitive and then the two are compared (JLS 15.21.2). If both are Boolean wrappers, then their references are compared just like in the case of other objects. Thus, new Boolean(“true”) == new Boolean(“true”) is false, but new Boolean(“true”) == Boolean.parseBoolean(“true”) is true.
for each
Used to iterate over an array or a Collections class
Syntax:
for (type var : array)
{
statements using var;
}
*Instead of declaring and initializing a loop counter variable, you declare a variable that is the same type as the base type of the array, followed by a colon, which is then followed by the array name.
In the loop body, you can use the loop variable you created rather than using an indexed array element
Limitations of for-each loop
1.For-each loops are not appropriate when you want to modify the array
2. For-each loops do not keep track of index.
3. For-each only iterates forward over the array in single steps
4. For-each cannot process two decision making statements at once
5. For-each also has some performance overhead over simple iteration:
The main methods
*execution of a class from command line requires the class to have the standard main method
public static void main (String[] args)
–> f the program is run without any arguments, args points to a String array of length 0 (args is NOT NULL!!!!)
Package import
“A compilation unit that has no package declaration is part of an unnamed package.” Thus, if you omit the package statement, the class will belong to the unnamed package.
Remember that classes in the unnamed package are accessible only to other classes in the unnamed package. It is not possible to import this unnamed package in classes that belong to a named package.