Methods Flashcards

1
Q

Encapsulation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  • Every class belongs to a package.

- A class may inherit from another class.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  • 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.
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  • 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.
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Encapsulation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Member fields are declared private and public accessor/mutator methods are provided to access and change their values if needed.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Overloading

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Private Access

A

Only code in the same class can call private methods or access private fields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Default (Package Private) Access

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Protected Access

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Public Access

A

Public means anyone can access the member from anywhere.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

static methods have two main purposes

A

■ 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Regular imports vs. Static imports

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

overloading

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Constructor

A

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");
     }
}
17
Q

Order of Initialization

A
  1. If there is a superclass, initialize it first.
  2. Static variable declarations and static initializers in the order they appear in the file.
  3. Instance variable declarations and instance initializers in the order they appear in the file.
  4. The constructor.
18
Q

Private constructor

A

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.

19
Q

this()

A

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.

20
Q

JavaBeans

A

JavaBeans are reusable software components. JavaBeans call an instance variable a property.

21
Q

A constructor cannot be final, static or abstract.

A

+

A constructor cannot return anything. Not even void.

22
Q

Multiple type inheritance

A

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.

23
Q

Multiple state inheritance

A

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.

24
Q

Java supports Pass by Value for everything

A

. 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.

25
Q

“pass-by-value”

A

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.

26
Q

Lambda

a -> a.canHop()

A

■ 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

27
Q

Lambda

(Animal a) -> { return a.canHop(); }

A

■ 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

28
Q

Functional interfaces

A

Interfaces that have only one method.

29
Q

Predicate

A
public interface Predicate {
boolean test(T t);
}

It’s in the package java.util.function