Mind map
Objects and Classes
Map content
Objects and Classes
What is an object and a class?
- Objects have attributes (fields) and can perform actions (methods)
- Fields are variables that describe objects. OR they can refer to another object
- Example: an Employee class can have an address field of type Address, which is a class that represents a street address: Address address
- Objects can contain other objects if they relate to each other
- Syntax: Every word besides the first word is capitalized
- Example: age, maxAge, numberOfRows
- A method has a declaration part (or signature) and a body
- Declaration part has a return value, method name, and a list of arguments
- To declare a method: returnType methodName (listOfArguments)
- 3 return types: a primitive, an object, void
- Void = return nothing
- Methods with multiple arguments are separated by a comma
- Example: int add(int a, int b)
- The body contains code that can perform the action
- A main is a special method that serves as the entry point to an application
- An application can have many classes but only one class needs to have a main method
- Declaration point of a the main: public static void main(String[ ] arga)
- An application can have many classes but only one class needs to have a main method
- Declaration part has a return value, method name, and a list of arguments
- Fields are variables that describe objects. OR they can refer to another object
- Classes are the blueprint needed to create objects. The class determines what objects you can have.
- 3 necessary components: the class name the fields, and the methods
- UML Class Diagrams summarizes all the fields and methods
- Can include field types and method signatures
- The field and its type is separated by a colon
- Method's argument list is presented in parenthases and its return type is written after a colon
- UML Class Diagrams summarizes all the fields and methods
- Declare a class by using keyword 'class" followed by the class name
- Example: class className {[class body]}
- Class names capitalize the initial of each word
- Example: Employee, DateUtility, RegularRateCalculator
- Class definitions must be saved in a file with the same name as the class name and also have the java extension
- Example: Employee class is saved as Employee.java
- Each class must have one constructor
- No return value (even void) and must have the same name as the class
- constructorName {listOfArguments
- May have a zero argument, called a no-argument (or no-arg) constructor
- Can be used to initialize the fields in the object
- No return value (even void) and must have the same name as the class
- 3 necessary components: the class name the fields, and the methods
- Objects have attributes (fields) and can perform actions (methods)
Creating objects in Java
- Most common: use keywrod 'new' collowed by the constructor of the class you want to create
- Example: new Employee();
- Note: assign the created object to an object variable so you can manipulate it later
- To do this: declare an object reference with the same type as the object
- Example: Employee employee = new Employee();
- To do this: declare an object reference with the same type as the object
- Once an object is created, you can call its methods and access its fields by using the object reference that was assigned to the object
- Use a period to call a method or field
- Example: objectReference.methodName
- Use a period to call a method or field
- Reference variables refer to objects but when they don't have a value, it's considered a null value
- Example: the class level reference variable is of type Book but hasn't been assigned a value: Book book; book is null
- If you declare a local reference variable in a method but don't assign an object, need to assign null to satisfy the compiler
- Example: Book book = null;
- Can test if a reference variable is null by using the == operator
- Most common: use keywrod 'new' collowed by the constructor of the class you want to create
Information
- Description
- Mind Map on Objects and Classes, created by Julianna Pettey on 08/09/2020.
- Created by
- Julianna Pettey · Sep 2020
- Updated
- Sep 10, 2020
- Views
- 2
- Rating