Mapa mental
METHODS AND ENCAPSULATION
Contenido del mapa
METHODS AND ENCAPSULATION
Designing Methods
- Access Modifiers
- Public
- The method can be called from any class
- Private
- The method can only be called from within the same class
- Protected
- The method can only be called from classes in the same package or subclasses
- Default (Package Private) Access
- The method can only be called in the same package.
- No Keyword
- The method can only be called in the same package.
- The access modifier its the first element in the method declaration
- Not required
- Public
- Optional Specifiers
- static
- Used for class methods
- abstract
- Used when not provinding a method body
- final
- Used when a method is not allowed to be overrided by a subclass
- Can have multiple specifiers in the same method
- Not all combinations allowed
- static
- Return type
- Might be an actual Java type
- if not return type: "void"
- permited to
- have a return statement with no value returned
- omit the return statement
- permited to
- Methods with a return type other than void
- required to have a return statement inside the method body
- needs to be assignable to the return type
- required to have a return statement inside the method body
- Method name
- same rules as identifiers
- by convention, methods begin with a lowercase letter
- same rules as identifiers
- Parameter list
- required
- doesn't have to contain any parameters
- Optional Exception List
- Can list as many types of exceptions as wanted
- Method body
- Access Modifiers
Working with Varargs
- must be the last element in a method's parameter list
- only allowed to have 1 per method
- Calling a method declaration with one vararg parameter, choice:
- Can pass in an array
- Can list the elements of the array and let Java create it
- Accesing a vararg parameter
- uses array indexing
Applying access modifiers
- Private
- Only code in the same class
- Default (Package private) Access
- Only classes in the package may access it
- Protected Access
- Allows everything that default allows and adds the ability to access members of a parent class
- "Extends"
- creates a subclass that has access to any protected or public members of the parent class
- rules applies in 2 scenerios
- A member is used without referring to a variable
- A member is used though a variable
- Public Access
- anyone can access the member from anywhere
- Designig Static Methods and Fields
- Static method don't require an instance of the class
- They are shared among all users of the class
- Static Methods have 2 main purposes
- for utility or helper methods that don't require any object state
- for state that is shared by all instances of a class
- Static method don't require an instance of the class
- Calling
- Usually just put the classname before the method
- System.out.println(Koala.count);
- Can use an instance of the object to call a static method
- compiler checks for the type of the reference
- Usually just put the classname before the method
- Static vs Instance calls
- Static method
- can calls another static method or variable
- just using the classname
- cannot call an instace
- can calls another static method or variable
- Instance method
- can call a static method or variable
- Using the classname or a reference variable
- can call another instance method or variable
- using a reference variable
- can call a static method or variable
- Static method
- Static variables
- some are meant to change
- Other are meant to never change
- constants
- uses "final" modifier
- Use all uppercase letters and underscores
- uses "final" modifier
- constants
- Static initialization
- add the static instance method or variable
- Static Imports
- are for importing static members of classes
- can use a wildcard
- import a specific member
- are for importing static members of classes
- Private
Passing Data Among Methods
- Java is a “pass-by-value” language
- a copy of the variable is made and the method receives that copy
- Assigning a new primitive or reference to a parameter doesn’t change the caller
- Getting data back from a method
- A copy is made of the primitive or reference and returned from the method
- Java is a “pass-by-value” language
Overloading Methods
- occurs when there are different method signatures with the same name but different type parameters
- can vary everything except method signature
- Varargs
- Java treats varargs as if they were an array
- public void fly(int[] lengths) { } public void fly(int... lengths) { }
- Java treats varargs as if they were an array
- Autoboxing
Creating Constructors
- special method that matches the name of the class and has no return type
- are used when creating a new object
- instantiation
- creates a new instance of the class
- new Car()
- creates a new instance of the class
- instantiation
- "this" keyword
- Most of the time is optional
- sometimes there are two variables with the same name
- parameter
- instance variable
- sometimes there are two variables with the same name
- tells Java you want to reference an instance variable
- Most of the time is optional
- Default Constructor
- has an empty parameter list and an empty body
- Having a private constructor in a class tells the compiler not to provide a default noargument constructor
- prevents other classes from instantiating the class
- Overloading Constructors
- the name is always the same since it has to be the same as the name of the class
- constructors must have different parameters in order to be overloaded
- "this"
- Java calls another constructor on the same instance of the class
- the name is always the same since it has to be the same as the name of the class
- Final Fields
- The constructor is part of the initialization process
- is allowed to assign final instance variables in it.
- The constructor is part of the initialization process
- Order of Initialization
- 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
Encapsulating Data
- variables of a class will be hidden from other classes
- can be accessed only through the methods of their current class
- data (an instance variable) is private
- getters/setters are public
- Immutable Classes
- They cannot be changed at all
- variables of a class will be hidden from other classes
Writing Simple Lambdas
- Functional programming
- is a way of writing code more declaratively
- specify what you want to do rather than dealing with the state of objects
- You focus more on expressions than loops
- uses lambda expressions
- block of code that gets passed around
- is a way of writing code more declaratively
- Lambda Syntax
- a -> a.canHop() == (Animal a) -> { return a.canHop(); }
- (String a, String b) -> { return a.equals(b); }.
- Predicate
- Is a common interface
- has one method named test that returns a boolean and takes any type
- package java.util.function
- removeIf() method
- Functional programming
Información
- Descripción
- Mapa Mental sobre METHODS AND ENCAPSULATION, creado por Enrique López Mata el 02/12/2018.
- Creado por
- Enrique López Mata
- Visitas
- 0
- Valoración