Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Practical Computer Science 2
03-OOP Concepts
Sayed Ahmad Sahim
Kandahar University
Computer Science Faculty
advjava19@gmail.com
February 19, 2019
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Conttents
1 Java Basics (OOP Concepts)
2 Class Relationships
3 Aggregation and Composition
4 Wrapper Class
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Class Abstraction and Encapsulation
• Java provides many levels of abstraction:
• class abstraction separates class implementation from how the
class is used
• separates class implementation from how the class is used.
• Class abstraction and encapsulation are two sides of the same
coin. Many real-life examples illustrate the concept of class
abstraction.
• For example your computer components such as RAM, CPU
and HDD
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Association
• Association is a general binary relationship that describes an
activity between two classes.
• For example, a student taking a course is an association
between the Student class and the Course class.
• A faculty member teaching a course is an association between
the Faculty class and the Course class.
• These associations can be represented in UML graphical
notation, as shown
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Association
• In Java code, you can implement associations by using data
fields and methods.
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Aggregation and Composition
• Aggregation is a special form of association that represents an
ownership relationship between two objects. Aggregation
models has-a relationships.
• The owner object is called an aggregating object, and its class
is called an aggregating class.
• The subject object is called an aggregated object, and its class
is called an aggregated class.
• If an object is exclusively owned by an aggregating object, the
relationship between the object and its aggregating object is
referred to as a composition.
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Example
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Processing Primitive Data Type Values as Objects
• Owing to performance considerations, primitive data type
values are not objects in Java.
• many Java methods require the use of objects as arguments.
• Java offers a convenient way to incorporate, or wrap, a
primitive data type into an object:
• double into the Double
• int into the Integer
• char into the Character
• Java provides Boolean , Character , Double , Float , Byte ,
Short , Integer , and Long wrapper classes in the java.lang
package
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Importance of Wrapper classes
• There are mainly two uses with wrapper classes.
1 To convert simple data types into objects, that is, to give
object form to a data type; here constructors are used.
2 To convert strings into data types (known as parsing
operations), here methods of type parseXXX() are used.
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Autoboxing and Unboxing in Java
• Autoboxing: Converting a primitive value into an object of the
corresponding wrapper class is called autoboxing.
• For example, converting int to Integer class. The Java
compiler applies autoboxing when a primitive value is:
• Passed as a parameter to a method that expects an object of
the corresponding wrapper class.
• Assigned to a variable of the corresponding wrapper class.
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
Unboxing
• Unboxing: Converting an object of a wrapper type to its
corresponding primitive value is called unboxing.
• For example conversion of Integer to int. The Java compiler
applies unboxing when an object of a wrapper class is:
• Passed as a parameter to a method that expects a value of the
corresponding primitive type.
• Assigned to a variable of the corresponding primitive type.
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
The BigInteger and BigDecimal Classes
• If you need to compute with very large integers or
high-precision floating-point val- ues, you can use the
BigInteger and BigDecimal classes in the java.math pack- age.
• Both are immutable.
• he largest integer of the long type is Long.MAX VALUE
9223372036854775807.
• An instance of BigInteger can represent an integer of any size.
MPFCS Sayed Ahmad Sahim
Java Basics (OOP Concepts) Class Relationships Aggregation and Composition Wrapper Class
MPFCS Sayed Ahmad Sahim

03- OOP Concepts.pdf

  • 1.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Practical Computer Science 2 03-OOP Concepts Sayed Ahmad Sahim Kandahar University Computer Science Faculty advjava19@gmail.com February 19, 2019 MPFCS Sayed Ahmad Sahim
  • 2.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Conttents 1 Java Basics (OOP Concepts) 2 Class Relationships 3 Aggregation and Composition 4 Wrapper Class MPFCS Sayed Ahmad Sahim
  • 3.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Class Abstraction and Encapsulation • Java provides many levels of abstraction: • class abstraction separates class implementation from how the class is used • separates class implementation from how the class is used. • Class abstraction and encapsulation are two sides of the same coin. Many real-life examples illustrate the concept of class abstraction. • For example your computer components such as RAM, CPU and HDD MPFCS Sayed Ahmad Sahim
  • 4.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Association • Association is a general binary relationship that describes an activity between two classes. • For example, a student taking a course is an association between the Student class and the Course class. • A faculty member teaching a course is an association between the Faculty class and the Course class. • These associations can be represented in UML graphical notation, as shown MPFCS Sayed Ahmad Sahim
  • 5.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Association • In Java code, you can implement associations by using data fields and methods. MPFCS Sayed Ahmad Sahim
  • 6.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Aggregation and Composition • Aggregation is a special form of association that represents an ownership relationship between two objects. Aggregation models has-a relationships. • The owner object is called an aggregating object, and its class is called an aggregating class. • The subject object is called an aggregated object, and its class is called an aggregated class. • If an object is exclusively owned by an aggregating object, the relationship between the object and its aggregating object is referred to as a composition. MPFCS Sayed Ahmad Sahim
  • 7.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Example MPFCS Sayed Ahmad Sahim
  • 8.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Processing Primitive Data Type Values as Objects • Owing to performance considerations, primitive data type values are not objects in Java. • many Java methods require the use of objects as arguments. • Java offers a convenient way to incorporate, or wrap, a primitive data type into an object: • double into the Double • int into the Integer • char into the Character • Java provides Boolean , Character , Double , Float , Byte , Short , Integer , and Long wrapper classes in the java.lang package MPFCS Sayed Ahmad Sahim
  • 9.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Importance of Wrapper classes • There are mainly two uses with wrapper classes. 1 To convert simple data types into objects, that is, to give object form to a data type; here constructors are used. 2 To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used. MPFCS Sayed Ahmad Sahim
  • 10.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Autoboxing and Unboxing in Java • Autoboxing: Converting a primitive value into an object of the corresponding wrapper class is called autoboxing. • For example, converting int to Integer class. The Java compiler applies autoboxing when a primitive value is: • Passed as a parameter to a method that expects an object of the corresponding wrapper class. • Assigned to a variable of the corresponding wrapper class. MPFCS Sayed Ahmad Sahim
  • 11.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class Unboxing • Unboxing: Converting an object of a wrapper type to its corresponding primitive value is called unboxing. • For example conversion of Integer to int. The Java compiler applies unboxing when an object of a wrapper class is: • Passed as a parameter to a method that expects a value of the corresponding primitive type. • Assigned to a variable of the corresponding primitive type. MPFCS Sayed Ahmad Sahim
  • 12.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class The BigInteger and BigDecimal Classes • If you need to compute with very large integers or high-precision floating-point val- ues, you can use the BigInteger and BigDecimal classes in the java.math pack- age. • Both are immutable. • he largest integer of the long type is Long.MAX VALUE 9223372036854775807. • An instance of BigInteger can represent an integer of any size. MPFCS Sayed Ahmad Sahim
  • 13.
    Java Basics (OOPConcepts) Class Relationships Aggregation and Composition Wrapper Class MPFCS Sayed Ahmad Sahim