GoConqr hat ein neues Design! Sag uns, wie es dir gefällt.

Mindmap

METHODS AND ENCAPSULATION

Enrique López Mata Englisch

Inhalt der Mindmap

METHODS AND ENCAPSULATION

  1. 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 access modifier its the first element in the method declaration
        • Not required
    • 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
    • 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
      • 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
    • Method name
      • same rules as identifiers
        • by convention, methods begin with a lowercase letter
    • Parameter list
      • required
      • doesn't have to contain any parameters
    • Optional Exception List
      • Can list as many types of exceptions as wanted
    • Method body
  2. 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
  3. 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
    • 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
    • Static vs Instance calls
      • Static method
        • can calls another static method or variable
          • just using the classname
        • cannot call an instace
      • 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
    • Static variables
      • some are meant to change
      • Other are meant to never change
        • constants
          • uses "final" modifier
            • Use all uppercase letters and underscores
    • 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
  4. 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
  5. 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) { }
    • Autoboxing
  6. 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()
    • "this" keyword
      • Most of the time is optional
        • sometimes there are two variables with the same name
          • parameter
          • instance variable
      • tells Java you want to reference an instance variable
    • 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
    • Final Fields
      • The constructor is part of the initialization process
        • is allowed to assign final instance variables in it.
    • 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
  7. 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
  8. 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
    • 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

Informationen

Beschreibung
Mindmap am METHODS AND ENCAPSULATION, erstellt von Enrique López Mata am 02/12/2018.
Aufrufe
0
Bewertung

Feedback

Worum geht es?

Wie gefällt dir das neue Design?

Dein Eindruck