Вы находитесь на странице: 1из 7

Section 1: Fundamental Object-Oriented Concepts 1.

1 Describe, compare, and contrast primitives (integer, floating point, boolean, and character), enumeration types, and objects. 1.2 Describe, compare, and contrast concrete classes, abstract classes, and interfaces, and how inheritance applies to them. 1.3 Describe, compare, and contrast class compositions, and associations (including multiplicity: (one-to-one, one-to-many, and many-to-many), and association navigation. 1.4 Describe information hiding (using private attributes and methods), encapsulation, and exposing object functionality using public methods; and describe the JavaBeans conventions for setter and getter methods. 1.5 Describe polymorphism as it applies to classes and interfaces, and describe and apply the "program to an interface" principle. 1.1 Describe, compare, and contrast primitives (integer, floating point, boolean, and character), enumeration types, and objects. An application consists of Variables and Program Codes that manipulates the Variables. Primitives are used to store Basic Data Types which are used to create more Advanced Objects Data Types. Java Primitives include int (integer), float (floating point), boolean (boolean), char (character), byte, short, long and double. In program source code, Integer refers to an object while int refers to a primitive that contains an integer. Primitive variables can only be set of read and also be accessed much faster than objects. int (Integer) stores a whole number and has a default value of 0 which occupies 32 bits in memory. It can stores a 32-bit signed two's complement non-floating point number. The maximum and minimum values are 2,417,483,647 and -2,417,483,648 respectively. float (floating point) stores decimal values with default value of 0.0f. It requires a 32bits of memory with maximum and minumum values of 3.4e(+38) and 1.4e(-45) respectively. boolean (boolean) stores a 1-bit value of true or false (default value). char (character) stores a single 16-bit Unicode character and requires 16-bits of memory. It has a default value of '\u0000' or 0. The maximum and minimum values are '\uffff' (65,535) and '\u0000' (0). It is the only unsigned Java primitive. Java has a built-in wrapper class for each primitive which converts a primitive to an object. The Wrapper Classes are Integer, Float, Boolean and Character. As of J2SE 5.0,

conversion between primitive and wrapper class is automatically performed (autoboxing and unboxing). Enumerations are a special data type that allows for a variable to be set to predefined constants. For example, enum TrafficLights { RED, YELLOW, GREEN } where enum is the keyword. Object can function as enumerations but the later is more reader and reduces developer's error. Java is Strongly Typed which means programmer needs to declare the data type of each variable expicitly. However, Java Virtual Machine (JVM) may converts the data types automatically such as placing an int variable value to a float variable. Developer may perform variables castings to different data types or objects by placing the new data type in parentheses in front of the variable. Data can only be cast to compatible data types else runtime exception will occur. Naming Conventions resulted in readable and better maintainable codes. Naming convention by Sun Microsystem: Class - name is to be noun; first letter be capitalised along with each internal word after the first; short and descriptive; for example: TrafficLight Variables - starts with a lowercase letter with each sequential internal word to be capitalized; short and meaningful names; can use 1-letter names for temporary variables; for example: cupContent Objects consists of data and methods. Data is the state of the object while Methods are used to perform actions on the data. Object size is dynamic at runtime. An object must be declared like the primitive and initialised or set to a value before it can be used. A Class is a blueprint used to create Object by using the new keyword in the program source code. During program runtime, Java Virtual Machine (JVM) recognises the new keyword and instantiate an object. For example, Train fastTrain = new Train(100, true) where Train is the class and fastTrain is the object created. An Array is a series of variables (either primitive data types or objects) of the same type that can accessed by an index , starting from 0. An array is always considered as an object eventhough it is an array of primitives. If an array is used in a loop, developer needs to watch out for array index that is out of bounds. An out of bounds index will cause the Java Virtual Machine (JVM) to throw an exception at runtime. Once the array size is declared, it cannot be changed. In other words, array size is not dynamic. For example, int[] electionResult = new int[100] .

1.2 Describe, compare, and contrast concrete classes, abstract classes, and interfaces, and how inheritance applies to them. A concrete class is a regular class that can be instantiated. An abstract class cannot be instantiated and must be extended. It may also contain abstract methods which are not implemented. The abstract keyword is used to indicate that it is an abstract class. They have valid method signature and must be overridden and implemented in the class that extends the abstract class. The purpose of an abstract method is to define the required functionality that any subclass must have. Interfaces are used to define a required set of functionalities from that classes that implement the interface. Unlike inheriting from base classes, a class is free to implement as many interfaces as needed by using the extends keyword, followed by a comma-delimited list of interfaces. A class that implements an interface is required to implement all of the methods defined in the interface. Any unimplemented methods will result in compilation errors. Generally, interfaces are used to create a standard public interface for similar items. The interface keyword is used to specify that it is an interface while the implements keyword is used to implement an interface. Inheritance allows general classes to be created and then be used as the foundation for multiple unique classes. In order words, unique classes are allowed to inherit the methods and instance variables of more general classes. Inheritance reduces codes redundancy and makes codes more maintainable. Inheritance enables Polymorphism to be used. A class can only extend 1 class where multiple inheritance is impossible. The extends keyword is used to inherits from a superclass to become a subclass. The subclass may then adds new methods and instance variables that are unique to the subclass. A class that extends another class may override any inherited method by defining another method of the same name with the same arguments as in the superclass. A class may override all, none or only some of the methods it inherits from a super class. A subclass may also execute the superclass overridden method by using the super keyword (opposite of this keyword). Object is the base class in Java programming language and does not need to be explicitly extended with the extends keyword.

1.3 Describe, compare, and contrast class compositions, and associations (including multiplicity: (one-to-one, one-to-many, and many-to-many), and association navigation. Composition and Association describe a class relationship which is formed between 2 objects with reference to the other. The reference is usually stored as instance variable and can be in 1 direction or bidirectional. An association relationship is a weak relationship of 2 objects where neither has direct dependency on the other for logical meaning. In other words, if the relationship is lost, both objects still retain the same meaning as they previously possesed. A composition relationship has a stronger tie than the association relationship. Composition means 1 object is composed (depends on) of another object or multiple objects. If the relationship is lost, the logical meaning of the objects will be lost or significantly altered. Normally, it is required to manage the objects life cycle. 4 Types of Class Relationships: 1. Direct Association 2. Composition Association 3. Aggregation Association 4. Temporary Association Direct Association describes a has-a relationship. It is associated with association relationship due to its weak relationship. This tends to be the default association when nothing else can accurately describe the relationship. Composition Association is associated with composition relationship due to its strong relationship. It is described as composed-of relationship. The containing object is responsible to manage the life cycle of the internal object. In other words, the containing object must make reference to the internal object else the Java Virtual Machine (JVM) will destroy it. If the containing object is destroyed, any referenced objects will be automatically destroyed as well.

Aggregation Association represents a part-of the whole relationship. It can still retain its own meaning independently even though the relationship is lost. Neither object depends on the other for its existence. It is generalized as an association relationship.

Temporary Association is also known as dependency. Typically, a temporary will be an object used as a local variable, return value or method parameter. It is the weakest form of association relationship and does not persist for the entire life cycle of the object.

Multiplicity exists in every object relationship which refers to the number of objects used as part of a relationship. 3 Types of Relationship Multiplicity: 1. 2. 3. 1-to-1 1-to-Many Many-to-Many

A 1-to-1 relationship is where 1 object contains a reference to another object. All 4 association relationships may be valid. A 1-to-Many relationship is where 1 object contains reference to a group of like objects which are normally stored in an array or a collection. A Many-to-1 relationship is also possible. All 4 association relationships may be valid. A Many-to-Many relationship is only possible for weak associations, namely aggregation, direct and temporary. It is not required to have an equal number of objects on each side of the relationship. Association Navigation refers to the direction in which a relationship can be traveled. An object that is contained within another object is said to be navigable if the containing object has methods for accessing the inner object. Its relationship can be either in 1 direction or bidirectional. 1.4 Describe information hiding (using private attributes and methods), encapsulation, and exposing object functionality using public methods; and describe the JavaBeans conventions for setter and getter methods. Encapsulation uses object to store related data and method together and enforces Information Hiding concept. It allows objects interactions while hiding the implementation details. Developer ends up with better maintainable codes. Access modifiers are the keywords that determine the accessibility of the class, methods and instance variables. Unauthorized access leads to program compilation error. 3 Types of Access Modifiers: Private - Can only be accessed within the same class (not by subclass) and it is most restrictive. Default (package-private) - Can be accessed within the same package. No keyword and it is used when the access modifier keyword is omitted. Protected - Can be accessed within the same package and also by its subclass outside of the package. Public - No restriction on its accessibility

Instance variable is usually set as private while public class methods such as Setters and Getters are used to interact with the outside world. JavaBeans naming convention is also used in the public class methods. In Setters method, it starts with a lowercase

set, followed by the variable name (first letter capitalized) with no spaces. Getters method use the same convention but starts with a lowercase get. However, there is an exeption when a boolean value is returned. In this case, is is used rather than get.

1.5 Describe polymorphism as it appplies to classes and interfaces, and describe and apply the "program to an interface" principle. Polymorphism word originates from Greeks which simply means many forms. In Java context, polymorphism means 1 object can take the place of an object of different type. Polymorphism via Class Inheritance occurs when a certain object type is required and an object of that type or its inherited object is accepted in its place. In this case, polymorphism utilizes the is-a relationship. A subclass is said to have an is-a relationship with its superclass. When an object is polymorphically acting as another object, the more specific object is restricted to only using the public interface of the more general object. The is-a relationship is unidirectional, ie., only the subclass can take the place of its superclass. Polymorphism via Interfaces occurs when a class implements an interface, it is required to implement all the methods in the interface. Hence, the class includes all the functionalities that the interface defines. This allows all objects instantiated from the classes to polymorphically behave as the data type of the interface. Programming to an interface concept specifies that the code should interact based on a defined set of functionality instead of an explicitly defined object type. In other words, it is better for the public interfaces of objects to use data types that are defined as interfaces than to a particular class. This allows code to be more abstract and flexible.

Вам также может понравиться