Creating Classes

--Originally published at diegotc2016

Creating Class Diagrams

We have to use a UML Diagram, which is written like this:

Creating Classes

Creating Classes

The – and + means that if it is private of public method, – is private and + is public.

Converting Class Diagrams into Code

Let’s make a example to demonstrate this:

This is my first time doing this so know that I might be wrong.

FootballPlayer

+position: String

-benchReps: Integer

+power(): String

+morePower(Integer)

Creating Classes

Exploring Object Lifetime

Making an object so that we can use the object is called instantiation. You create an object in Java by doing this:

Creating Classes

Constructor is a special method that exists to construct the object. The constructors are still part of the instantiation of the object. You can make a constructor on Java by making a method in the class with the same name of the class, for example:

Creating Classes

We also have descructor or a finalizer which are used when an object isn’t needed any more.

Using Static or Shared Members

Static in programming means that it never changes and it could be used in back accounts for example:

Creating Classes

The interest rate is the same for every bank account it doesn’t matter if the account is from Alice, Joe or Sam.

This is how static will look in java:

Creating Classes

If you want to access to an attribute of an object you type first the name of the object and then the type of attribute, for example: samAcct.accountNumber.

However if you want to acces to a static object you type first the name of the class and then a dot and then the name of the static object, for example: SavingsAccount.interestRate

You can also create a static method by just adding the word static in it, static method can only work with static variables. Static objects or methods are identified in UML diagrams by underlined words.


Creating Classes