Java Class
Java Beans, Encapsulation, & inheritance
Class
consists of: member variables/properties/fields & methods
public class should be only one in a source file.
main() should be in public class
overloading
: making multiple constructors.
this: the class itself
super: the parent class
{class name}() is a constructor.
this() is the class constructor itself.
super() is the parent class constructor.
call ‘this()’ in multiple constructors (same name, different parameters)
The parameters can have the same name as member variables. Variables in a method with the same name as member variables are valued higher. Use ‘this’ for outside variables (member variables).
Instance vs Object:
An instance is an initialized object from a class or using a constructor.
Constructor vs Setter:
Constructor for initialization, setters for update.
Initializing block
static {} :runs when the class gets into memory
{} :when an instance is created
Get mk8506’s stories in your inbox
Join Medium for free to get updates from this writer.
-> used for initializing static / default member variables, but constructors do the work, so not in use usually.
JavaBeans
: specific classes with the characteristics defined by Oracle
- Public default constructor
- Private member variables
- Public accessors (getters & setters)
- Serializable (converting into a byte stream)
- Event support
More ideologically fixed names for specific classes or instances: FOJO, VO, & DTO
Encapsulation
: hiding
- public -> use for all methods
- private -> use for all member variables
- protected
- default
Inheritance
- extends {parent class}
- can override
- parent’s constructor
- more methods if you want
uml: drawing of class inheritance
# {name}: {type}
<<constructer>>
polymorphism (type casting)
Downcasting:
- int i = (int) doubleNum;
2. if (a instanceOf b)
Upcasting:
Useful when:
Animal[] ani; ←- Animal eagle = new Eagle();
Extension
1. code generator for java
>generate constructer using all fields
>generate getters setters
2. plantuml
@startuml {name}
scale 2
@enduml
>uml
(ctrl . ·)




