Mapa mental
Objects

Conteúdo do mapa
Objects
How are they represented in life?
- Any thing you can think of: abstract or concrete
- Ex: people, animals, streets, houses, apples, love, trees, laws
What features do they have?
- Attributes and actions they can perform
- Attributes are descriptors
- Ex for a car: color, # of tires, plate number, # of valves
- Actions they can perform
- Ex for a car: run, brake
- Attributes are descriptors
- Attributes and actions they can perform
How are they represented in Java?
- actions are called methods
- Both are optional. Object may have one, both, or neither of a method or field.
- attributes are called fields
- actions are called methods
How do you create Objects in Java?
- Classes!
- "A class is a blueprint or a template to create objects of identical type"
- To create "Employee" objects, you must first create an "Employee" class
- Ex: Creating an "Employee" class that has fields for age and position, then all "Employee" objects created out of this class will have those fields
- To create "Employee" objects, you must first create an "Employee" class
- Classes are the abstraction of a problem, with the objects being representative of real life objects
- All program elements must reside in a class in Java
- 3 things to consider when writing a class
- the class name
- The Fields
- Fields are variables
- Can be primitives or references to objects
- Ex of reference to another object: Address address;
- in which an "Employee" class has an "address" field of type "Address"
- Ex of reference to another object: Address address;
- Can be primitives or references to objects
- initial of each word after the first is capitalized
- Ex: validAddress, numberOfRows
- Fields are variables
- The Methods
- Define actions that a class's objects (instances) can do
- syntax
- a method has a declaration part and a body
- declaration part consists of: a return value, method name, and a list of arguments.
- AKA "signature"
- the body contains code to perform the action
- declaration part consists of: a return value, method name, and a list of arguments.
- Ex of a getSalary method that returns a double:
- double getSalary ( )
- "the getSalary method does not accept arguments" (I am confused)
- double getSalary ( )
- Ex of a method that returns an Address object"
- Address getAddress ( )
- Ex of a method that accepts an argument:
- int negate (int ~an actual number~)
- Ex of a method that takes more than one argument (separate them by a comma):
- int add (int a, int b)
- this "add" method will take 2 "ints" and return an "int:
- int add (int a, int b)
- Ex of a getSalary method that returns a double:
- a method has a declaration part and a body
- the return type of a method can be primitive, an object, or void
- return type "void" means that the method returns nothing
- The Method main
- provides an entry point to an application. An application only needs one class to have a main method
- the signature goes:
- public static void main (String [ ] ~args~)
- can pass arguments to "main" when using "java" to run a class by typing them after the class name and separating them with a space
- java ~className~ ~arg1~ ~arg2~ ~arg3~ ...
- Ex passing two arguments "1" and "safeMode" when running Test class:
- java Test 1 safeMode
- Ex passing two arguments "1" and "safeMode" when running Test class:
- java ~className~ ~arg1~ ~arg2~ ~arg3~ ...
- the syntax
- Example for an employee
- Example for an employee
- class names capitalize the initial of each word
- Ex: DateUtility
- class definitions saved in a file with the same name as the class name
- Ex: Employee.java
- "age" and "salary" are fields
- Ex: Employee.java
- Constructors
- Used to construct an object and looks like a method
- Every class needs at least one constructor
- does not have a return value
- a constructor must have the same name as the class
- syntax
- Ex adding two constructors to Listing 4.1:
- second constructor passes the values at the same time you create the object
- instead of:
- instead of:
- second constructor passes the values at the same time you create the object
- "A class is a blueprint or a template to create objects of identical type"
- Creating Objects AKA constructing/instantiating instances (wow!)
- using the "new" keyword, followed by a constructor of the class to be instantiated
- Ex to create an "Employee" object
- new Employee ( ) ;
- Usually want to assign the new object to an object/reference variable
- Ex:
- Employee employee = new Employee ( ) ;
- where employee is an object reference of type Employee
- Employee employee = new Employee ( ) ;
- once you have object, can call its methods and access its fields through the assigned object reference. Use a period to call a method or field
- Ex creating an "Employee" object and assigning values to its "age" and "salary" fields
- Ex creating an "Employee" object and assigning values to its "age" and "salary" fields
- Reference variables can also not contain values AKA not reference an object
- Ex of type "Book" not assigned a value
- Book book; // Book is null
- Ex of local reference variable within a method without an assigned object:
- Book book = null
- Can’t access the field or method of a null variable, leads to error.
- Ex of type "Book" not assigned a value
- Ex:
- Ex to create an "Employee" object
- using the "new" keyword, followed by a constructor of the class to be instantiated
- Classes!
Notes
- can not italicize individual words on here
- ~'s surrounding a word means to italicize it
- white colored node contains code
- can not italicize individual words on here
AKA instances
Imagens e arquivos do mapa
Informações
- Descrição
- Computer Science Mapa Mental sobre Objects, criado por Andre Luck em 30-05-2020.
- Criado por
- Andre Luck
- Visualizações
- 0
- Avaliação







