Methods Flashcards
Encapsulation
Encapsulation is the technique used to package the information in such a way as to hide what should be hidden, and make visible what is intended to be visible. In simple terms, encapsulation generally means making the data variables private and providing public accessors.
- Every class belongs to a package.
- A class may inherit from another class.
true
- It is not required for a class to have a main method. The main method is required only if you want to execute that class directly from a command line.
- A package may have just one class as well.
true
- Protected things (methods and fields) can be accessed from within the package and from subclasses.
- Public things (classes, methods and fields) are accessible from anywhere.
- No modifier means package (or default access) and is only accessible inside the package.
true
Encapsulation
Encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition.
Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state. A benefit of encapsulation is that it can reduce system complexity, and thus increases robustness, by allowing the developer to limit the interdependencies between software components.
Member fields are declared private and public accessor/mutator methods are provided to access and change their values if needed.
true
Overloading
Overloading of a method occurs when the name of more than one methods is exactly same but the parameter lists are different. If there is another method with the same name but with a different number of arguments in a class then that method can be called as overloaded.
Private Access
Only code in the same class can call private methods or access private fields.
Default (Package Private) Access
When there is no access modifer, Java uses the default, which is package private access. This means that the member is “private” to classes in the same package. In other words, only classes in the package may access it.
Protected Access
Protected access allows everything that default (package private) access allows and more.
The protected access modifer adds the ability to access members of a parent class.
Public Access
Public means anyone can access the member from anywhere.
static methods have two main purposes
■ For utility or helper methods that don’t require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.
■ For state that is shared by all instances of a class, like a counter. All instances must share the same state. Methods that merely use that state should be static as well.
Regular imports vs. Static imports
Regular imports are for importing classes.
Static imports are for importing static members of
classes. Just like regular imports, you can use a wildcard or import a specifc member.
Java uses pass-by-value to get data into a method.
Assigning a new primitive or reference to a parameter doesn’t change the caller. Calling methods on a reference to an object does affect the caller.
true
overloading
Everything other than the method signature can vary for overloaded methods. This means there can be different access modifers, specifers (like static), return types, and exception lists.
Constructor
Constructor is a special method that matches the name of the class and has no return type.
public class Bunny { public Bunny() { System.out.println("constructor"); } }
Order of Initialization
- If there is a superclass, initialize it first.
- Static variable declarations and static initializers in the order they appear in the file.
- Instance variable declarations and instance initializers in the order they appear in the file.
- The constructor.
Private constructor
Having a private constructor in a class tells the compiler not to provide a default noargument constructor. It also prevents other classes from instantiating the class. This is useful when a class only has static methods or the class wants to control all calls to create new instances of itself.
this()
When this is used as if it were a method, Java calls another constructor on the same
instance of the class.
If you choose to call it, the this() call
must be the frst noncommented statement in the constructor.
JavaBeans
JavaBeans are reusable software components. JavaBeans call an instance variable a property.
A constructor cannot be final, static or abstract.
+
A constructor cannot return anything. Not even void.
Multiple type inheritance
Java allows a class to implement multiple interfaces. An interface is a “type” and does not contain any state. This implies that Java supports multiple type inheritance.
Multiple state inheritance
A class contains state and extending a class means inheriting the state. Since Java does not allow a class to extend from multiple classes, it means that Java does not support multiple state inheritance.
Java supports Pass by Value for everything
. Primitives are always passed by value.
. Object “references” are passed by value. So it looks like the object is passed by reference but actually it is the value of the reference that is passed.
“pass-by-value”
Java is a “pass-by-value” language. This means that a copy of the variable is made and the method receives that copy.
Assigning new values to those parameters in the method doesn’t affect the caller’s variables.
Lambda
a -> a.canHop()
■ Specify a single parameter with the name a
■ The arrow operator to separate the parameter and body
■ A body that calls a single method and returns the result of that method
Lambda
(Animal a) -> { return a.canHop(); }
■ Specify a single parameter with the name a and stating the type is Animal
■ The arrow operator to separate the parameter and body
■ A body that has one or more lines of code, including a semicolon and a return statement
Functional interfaces
Interfaces that have only one method.
Predicate
public interface Predicate { boolean test(T t); }
It’s in the package java.util.function