Module 1: Classes & objects Flashcards
What type of language is java?
Object oriented programming, this is intended to make coding feel like thinking in real life.
What is an object and how do you create one?
An object is an instance of a class. Object creation syntax is below:
Syntax:
(class name) (object name) = new (class name) ();
Ex: Banana is an instance of fruit.
Ex creating:
public class Animal {}
{Animal Cat = new Animal();
What is an attribute?
Essentially just a variable within a class.
Ex:
public class car {
int topSpeed;
String color;
String engine;}
What does the dot notation do?
Allows access to the objects attributes and methods.
Ex method:
Cat.Sound1();
Ex attribute:
Cat.color = “Brown”;
What are the access modifiers?
Classes:
- public (The class is accessible by any other class)
- default {Left blank} (The class is accessible only by classes in the same package)
Attributes/Methods/Constructors:
- public (Accessible from any other class)
- private (Accessible only within the declared class itself)
- default {Left blank} (If left blank, is available to any other class in the same package)
- protected (Same as default, with the addition of subclasses being able to access protected methods and attributes of the superclass)
What are the non access modifier keywords?
Classes:
- final (The class cannot be inherited by other classes)
- abstract (The class cannot be used to create objects) To access an abstract class, it must be inherited from another class.
Attributes/Methods:
- final (Attributes and methods cannot be overridden/modified
- static (Attributes and methods belong to the class, not the object/class instance, and they can be called without creating an object)
- abstract (Can only be used in abstract classes, and only used on methods)
- transient (Attributes and methods are skipped when serializing the object containing them)
- synchronized (Methods can only be accessed by one thread at a time)
- volatile (The value of an attribute is not cached thread locally, and is always read from the “main memory”)
It’s good practice to keep attributes within a class private.
What are getters and setters?
Get method returns the value of an attribute, set method takes a parameter and changes an attribute.
Syntax:
get(attribute name capitalized)()
set(attribute name capitalized)(parameter)
To access private attributes in another class, create public get and set methods.
Ex:
public class Person {private String name; public String getName() {return name;}
public void setName(String n) {this.name = n;}}
public class Main {public static void main(String[] args) {Person myObj = new Person(); myObj.setName(“Kieryn”);
System.out.println(myObj.getName());}}
The “this” keyword refers to the current object.
What does a constructor do?
Special methods invoked when an object is created with the new keyword and is used to initialize the object. They can take parameters to initialize attributes, and you can have as many constructors as you want.
Ex:
public class Car {private String color;
Car() {this.setColor(“Red”);}
Car(String c) {this.setColor(c);}
public void setColor(String c) {this.Color = c;}
public String getColor() {return color;}}
public class Program {public static void main(String[] args) {Car v1 = new Car();
Car v2 = new Car(“Green”);
System.out.println(v2.getColor());}}
Constructor rules:
- The name must be the same as the class
- They are not allowed to have any explicit return type (they also cannot be void)
What are the primitive data types, and reference types?
Primitive data types:
- byte
- short
- int
- long
- float
- double
- boolean
- char
Reference data types:
- String
- Array
What is the difference between primitive data types and reference data types?
Primitive types are passed by value, if you pass a primitive type to a method, it won’t change the variables value because the method receives a copy of the variables value.
Ex:
public static void main(String[] args) {
int x = 4;
square(x);
System.out.println(x);}
static void square(int x) {x = x*x;}
Reference data types point to the address (memory location) where the data is stored.
What does the static keyword do?
Indicates an attribute or method belongs to the class, not a specific instance of a class (object). Static method can be called without creating an object.
Ex:
public class Car {public static void horn() {System.out.println(“Beep”);}}
public class Test {public static void main(String[] args) {Car.horn();}}
It is good practice to name a static attribute in all caps.
What is a package?
A group of classes, they are used to avoid name conflicts and to control class access. Packages should be named in all lowercase, and to use a package you must import it.
Ex:
import java.util.Scanner;
You can also import all classes of a package with a wildcard.
Ex:
import java.util.*;
When you have a class in a package, the following code appears at the very top of a class:
package (name);