Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Wednesday, February 20, 2013

Class declaration

Every Java class begins with a class declaration, as follows:

public class MyClass {

}

public and class are reserved Java keywords and you may not use them to name entities in your own library files.

MyClass is the name of the class - this can be anything you like, however:
  • must not contain spaces - rule
  • should start with an uppercase letter - convention
  • should not start with a number - convention
  • where there are multiple words in the class name, the first letter of each word should be capitalised (also known as Camel-Case) - convention
Note: A rule applies to language syntax and cannot be broken. Convention applies to common standards and these can be broken.

Every class declaration includes a set of curly braces - one that opens a block, and one that closes the block. All code for the class must be contained within this set or the code will fail.

Another example:

public class NetworkUser {

}

This class is called NetworkUser.
 

Tuesday, January 8, 2013

Access Modifiers | Private, Public & Protected


Access Modifiers | Private, Public & Protected

An access modifier determines the visibility of a class member (states & behaviours).



An access modifier of Public allows a class member to be accessed from external objects & is denoted on a class diagram with a + (addition) symbol.

A Private access modifier allows only internal access and prevents access by external objects. Private access is denoted on a class diagram with the - (minus) symbol.

The Protected access modifier allows internal access and access by child classes. Protected access is denoted on a class diagram with the # (hash) symbol.

Saturday, September 15, 2012

Sunday, May 27, 2012

Visibility

"Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/JgfUgH )

Tuesday, April 24, 2012

Constructors and instantiation

"Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/I67fRB )

Every class has at least one Constructor. This is a special method that prepares the object for use. It has the same name as the class.


A class can have multiple constructors - this is known as method overloading.

The constructor is always the first method called on an object. Think of it as an 'on-switch'. When we instantiate an object of a class, we call it's constructor using the new keyword:

Syntax:
Type identifier = new Type(parameters, if any);

Example:
Telephone myTelephone = new Telephone();


In UML notation, the constructor is always referred to as new(). This is because UML is language independent and not Java specific. Different programming languages have different rules for naming a constructor.

Monday, March 19, 2012

What is a Class? What is an object?

A class is a definition of all state/attributes & behavior belonging to an entity. The following figure is a UML class diagram for an entity called Player. The middle compartment lists each state/attribute of Player and the bottom compartment lists each behaviour of Player.

UML class diagrams are used to design classes. This design is then used to inform the code. It's like a blueprint for building a house.

Objects are created from classes - many objects may be created from a single class - in the same way as one blueprint can build many houses. The following figure is a UML object diagram showing the Player class along with 3 Player objects.

Notice that while each object has a strength and agility attribute, the values for these attributes are unique from object to object. From this diagram, we can see the state of each object.

The behaviours are not listed in the object diagram because all objects share the same behaviours.

"What Is a Class? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)" ( http://bit.ly/FUAjmF )

Sunday, March 18, 2012

The Main method

A Java project many contain many packages and each package may contain many classes. There must be at least one class containing a main method.

The main method is a static method, belonging to the class, where program execution begins - it's the front door, so to speak. The main method begins the chain of events that load the program into memory, ready for use.

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)


The main method can optionally take a String array as a parameter, as shown in the parenthesis.

(String[] args)