Sitemap
mk8506

mk8506

Java Class

2 min readSep 29, 2024

--

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

  1. Public default constructor
  2. Private member variables
  3. Public accessors (getters & setters)
  4. Serializable (converting into a byte stream)
  5. Event support

More ideologically fixed names for specific classes or instances: FOJO, VO, & DTO

Encapsulation

: hiding

  1. public -> use for all methods
  2. private -> use for all member variables
  3. protected
  4. default

Inheritance

  1. extends {parent class}
  2. can override
  3. parent’s constructor
  4. more methods if you want

uml: drawing of class inheritance

# {name}: {type}
<<constructer>>

polymorphism (type casting)

Downcasting:

  1. 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 . ·)

--

--