0% found this document useful (0 votes)
11 views

BSCS 208 - AOOP - Lecture 2b - Behavioral Implementation of Aspects of Classes

The document discusses object-oriented programming concepts like static members, nested methods, composition, aggregation, associations, and constructor overloading in Java. Static members are common to all objects of a class and can be accessed without creating an object. Nested methods can call other methods in the same class. Composition involves combining objects to form a larger object, while aggregation and association describe different types of object relationships.

Uploaded by

craigcarlos95
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

BSCS 208 - AOOP - Lecture 2b - Behavioral Implementation of Aspects of Classes

The document discusses object-oriented programming concepts like static members, nested methods, composition, aggregation, associations, and constructor overloading in Java. Static members are common to all objects of a class and can be accessed without creating an object. Nested methods can call other methods in the same class. Composition involves combining objects to form a larger object, while aggregation and association describe different types of object relationships.

Uploaded by

craigcarlos95
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

BSCS 208: Advanced

Object Oriented
Programming

Lecture 2b - Behavioral
implementation of aspects
of classes

1
Static members
 A class contains variables (implementation)
and methods (interface)
 These are called instance variables and
instance methods because each time the
class is instantiated, a new copy of each of
them is created
 They are accessed using the objects of the
class together with the dot operator

2
Static members – cont’d
 We can define a member that is common
to all objects and which is accessed
without using a particular object
 This means that the member belongs to
the class as a whole rather than the
objects created from the class

3
Static members – cont’d
 To do that, we have to declare it as static
e.g.
• Static int count;
• Static int max(int x, int y);

4
Static members – cont’d
 Static variables and static methods are
called class variables and class methods
respectively since they are associated
with the class itself
 In addition to the ability to be called
without using objects, class methods are
also available to external classes

5
Static members – cont’d
 Java has many inbuilt class methods and
class variables e.g. the Math class has a
static method called sqrt() which can be
called as follows:
• float x = Math.sqrt(25.0);

6
Example: Defining and using
static members
class MathOperation {
static double mul(double x, double y) {
return x * y;
}
static double divide(double x, double y) {
return x/y;
}
}

7
Example: Defining and using
static members – cont’d
class MathApplication {
public static void main(String args[]) {
double a = MathOperation.mul(4.0, 5.0);
double b = MathOperation.divide(a, 2.0);
System.out.println("b = "+b);
}
}

8
Note
 No object was created
 Restrictions for static methods are-
• They can only call other static methods
• They can only access static data
• They cannot refer to this or super
keywords

9
Nesting methods
 We said an object can be called only by
an object of the class using the dot
operator or by the class itself in case it is
static
 There is an exception to this-
• A method can be called using only its name
by another method of the same class

10
Nesting methods – cont’d
 This is called nesting of methods
(common in other programming
languages like VB)
 In the example below the method
display() calls largest() to determine the
largest of two numbers and then displays
the result

11
Nesting methods – cont’d
class Nesting {
int m, n;
Nesting(int x, int y) {
m = x;
n = y;
}
int largest() {
if(m >= n)
return(m);
else
return(n);
}

12
Nesting methods – cont’d
void display() {
int large = largest();
System.out.println("The largest value = "+large);
}
}

13
Nesting methods – cont’d
class NestingTest
{
public static void main(String args[])
{
Nesting nest = new Nesting(50, 40);
nest.display();
}
}

14
Composition
 Composition is the process of having an
object that contains other objects e.g.
• A PC contains a keyboard, mouse, monitor
etc.
 These are separate objects but we
compose them together to form a larger
object

15
Composition – cont’d
 A composition relationship is considered
a “has-a” e.g. PC has a monitor
 An inheritance relationship is considered
a “is-a” relationship e.g. a cow is a type
of mammal
 Two types of composition are
• Association
• Aggregation
16
Composition – cont’d
 Aggregation is represented by lines with a
diamond e.g.
• A tyre as part of a car.
 The diamond is on the side of the car to
mean that the car has a tyre
 Association is represented by a line with no
diamond e.g.
• Standalone keyboard servicing a separate
computer

17
Composition – cont’d
 Inheritance produces another class which is
more specific and more functional while
composition is actually an interaction
 Composition produces a class which is more
complex than the parts
 Composition helps in abstracting things and
is useful for our minds since we cannot keep
a lot of things in the short-term memory e.g.
• Rather than say we have a large unit with a steering wheel,
four tyres, an engine etc. we say we have a car

18
Composition – cont’d
 Both represent object collaboration
 Their difference is-
• In aggregation, you see the whole
• In association, you see the parts that make up the
whole

19
Aggregations
 Means a complex object is composed
by other objects
 Examples:
• TV - is seen as one whole system, one neat
package, we do not see its parts
• Car – is seen as a whole, not as parts

20
Associations
 Here the focus is on parts that make up
the whole
 Example:
• Computer parts like the keyboard, mouse etc
constantly communicate with the CPU, but
are considered an external device.

21
Constructor overloading
class Room {
double length, breadth;
Room(double x, double y) {
length = x;
breadth = y;
}
Room(double x) {
length = breadth=x;
}
double area() {
return(length*breadth);
}}

22
Constructor overloading – cont’d
class RectangleClass {
public static void main(String args[]) {
Room room1 = new Room(25.0, 15.0);
Room room2 = new Room(20.0);
double area1 = room1.area();
double area2 = room2.area();
System.out.println("Area of Rectangle = "+area1);
System.out.println("Area of Square = "+area2);
} }

23

You might also like