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.