Classes and Objects Video
Gotta do HW
Lesson Notes
5.1 Anatomy of a class
- class: blueprint for objects, with instance variables, constructors, methods
- objects are instances of classes
- naming conventions: start uppercase letter, should be noun, not acronym
Methods define functionality/behavior of an object, can access instance variables
ACCESS MODIFIERS:
- public: no restrictions, can be accessed outside the class (used for classes and constructors)
- private: only in class (instance variables) methods can be designated with these
Benefit of modifiers: can limit access, and use getters/setters methods in order to interact with variables instead of directly editing the variable itself
5.2 Constructors
- initializes instance variables when object created
- usually 2 or more constructors
- can have multiple constructors with different parameters
- no return because it is creating an object and its variables, not calling methods to do work and return a value
- this keyword refers to the current object being called, usually being used to find the current object's variable or run a method on it
5.5 Modifiers/Accessor Methods
more specifically, they are getters/setters. They allow other classes to change the instance variables of a different class. It also is used to get private variables in an object
Mutator Methods
- they are a set method to change the value of the private variable. They don't return a value, but are void methods
Public vs Private vs Protected
- public is available for class and subclasses and everything in the world/project
- protected isnt available in the world but is available in the class
- private is only used in the class see example in https://supermengman.github.io/blog/jupyter/java/collegeboard/frq/student/2022/10/23/Unit-3-HW.html
5.8 Scope and Access:
- Class level is for instance variables
- Method level is for local variables
- Block level is for loop variables
Extra
- a main method in an abstract class can be used for tester methods, making sure that variables are set correctly. https://github.com/rjawesome/CSASpringTri2/blob/steptrack2/src/main/java/com/nighthawk/spring_portfolio/mvc/steptrack1/Person.java
-
a class can inherit from a parent class, and write new functions. see https://supermengman.github.io/blog/jupyter/java/collegeboard/2022/09/20/Fibonacci.html
- also subclass, super in there
- subclass constructor calls main constructor with the super keyword (super calls the parent function methods)
- also see override, with the @override allowing changes to the init function (overriding the parent)
- also subclass, super in there
-
abstract class/method: a restricted class that gets inherited from another class. methods can be in abstract classes, and are called when they are extended. This helps achieve security
- it is an implementation of polymorphism, where one class can have different implementations of a method. ```java // Abstract class abstract class Animal { // Abstract method (does not have a body) public abstract void animalSound(); // Regular method public void sleep() { System.out.println("Zzz"); } }
// Subclass (inherit from Animal) class Pig extends Animal { public void animalSound() { // The body of animalSound() is provided here System.out.println("The pig says: wee wee"); } }
class Main { public static void main(String[] args) { Pig myPig = new Pig(); // Create a Pig object myPig.animalSound(); myPig.sleep(); } } ```
Methods:
- overloading method: If there are different types in the parameters, you can cwrite multiple methods with the same name, as they still count.
class Calculate
{
void sum (int a, int b)
{
System.out.println("sum is"+(a+b)) ;
}
void sum (float a, float b)
{
System.out.println("sum is"+(a+b));
}
public static void main (String[] args)
{
Calculate cal = new Calculate();
cal.sum(8,5); //sum(int a, int b) is method is called.
cal.sum(4.6f, 3.8f); //sum(float a, float b) is called.
}
}
Standard Methods:
- toString: a method to print out all the attributes of an object
- equals(): a method to check if two objects and their attributes are equal (based on own criteria)
- hashCode(): a method to generate a unique identifier for an object
All can be lomboked in @Data, like in https://github.com/rjawesome/CSASpringTri2/blob/steptrack2/src/main/java/com/nighthawk/spring_portfolio/mvc/steptrack1/Person.java
Late Binding with superclasses
- early binding is just static, meaning that it compiles and binds the object to the class definition declared
- late binding means that it is bound to the child one or whatever is called in the new
public class NewClass {
public static class superclass {
static void print()
{
System.out.println("print in superclass.");
}
}
public static class subclass extends superclass {
static void print()
{
System.out.println("print in subclass.");
}
}
public static void main(String[] args)
{
superclass A = new superclass();
superclass B = new subclass();
A.print();
B.print();
}
}
NewClass.main(null);