C4 OBJECT – ORIENTED PROGRAMMING ( Part 2 )
Page 1 of 1
C4 OBJECT – ORIENTED PROGRAMMING ( Part 2 )
Combine:
• Example:
Rectangle rectOne = new Rectangle();
Rectangle rectTwo = new Rectangle(“Yellow”, 2.5, 11.6);
• Explanation:
o The rectOne and rectTwo are the Rectangle type and contains the reference to that object.
o For simplicity, to say that rectOne and rectTwo are the Rectangle objects.
o Rather than say that rectOne and rectTwo are the variables that contain a reference to Rectangle objects.
o Refer to Figure 4.3 to see the illustration of reference variable after the object is created in memory.
[You must be registered and logged in to see this image.]
Figure 4.3: States of the object (rectOne) after data has been stored in it
Note:
o An object is an instance of a class. You can create many instances of a class. Creating an instance is referred to as instantiation. The terms object and instance are often interchangeable.
o The relationship between classes and objects is analogous to the relationship between nasi lemak recipes and nasi lemak. You can make as many nasi lemak as you want from a single nasi lemak recipe.
Accessing an Object’s Data and Methods
After an object is created, its data can be accessed and its method invoked using the following
syntax: (using dot operator)
• Data/member: (if member is public)
objectname.data
• Method:
objectname.method
A Demo Program
Since Program 1 is not an application program (but it is a blueprint to create Rectangle objects), other programs will use the Rectangle class to create rectangles. The programs that create and use these objects will have their own main methods. Therefore, we need a demo program to test our class program.
Program 2 demonstrates some of the methods of the Rectangle class.
[You must be registered and logged in to see this image.]
Program 2: TestRectangle.java
Note:
• Program 2 must be saved in the same folder or directory as the file of Rectangle.java
• When the compiler reads the source code for TestRectangle.java and sees that a class named Rectangle is being used, it looks in the current folder/directory for the file Rectangle.class. (if it is not exists, compiler searches the file of Rectangle.java and compiles it)
CHECKPOINT
Q1 - What is the output of Program 2?
Q2 – What is the color of rectOne?
Q3 – How to write a statement to print rectTwo’s perimeter ?
Note:
• A main class (Rectangle) and a demo program (TestRectangle) can be combined in one file.
• BUT only one class can be public class. Public class must have the same name as the file name with the main method.
• Refer to Program 3. Check out the code at line 11.
[You must be registered and logged in to see this image.]
Program 3: Combine a class program and demo program
Modifiers
Java provides several modifiers that control access to data, methods and classes. This section
introduces the public, private and default modifiers.
• public
- defines classes, methods, and data in such way that all programs can access them.
• private
- defines classes, methods, and data in such way that they can be accessed by the declaring class but not by any other class.
• If public and private is not used, then by default the classes, methods and data are accessible by any class in the same package. This is known as package-private or package-access.
Refer the Figure 4.4 to understand this concepts
[You must be registered and logged in to see this image.]
• The private modifier restricts access within a class, the default modifier restricts access within a package, and the public modifier enables unrestricted access.
An object can access its private members if it declared in its own class. Refer to
Figure 4.6(a) & (b).
[You must be registered and logged in to see this image.]
Note:
(a) : This is OK because object s is used inside the ABC class
(b) : This is wrong because x and add are private in ABC
• Example:
Rectangle rectOne = new Rectangle();
Rectangle rectTwo = new Rectangle(“Yellow”, 2.5, 11.6);
• Explanation:
o The rectOne and rectTwo are the Rectangle type and contains the reference to that object.
o For simplicity, to say that rectOne and rectTwo are the Rectangle objects.
o Rather than say that rectOne and rectTwo are the variables that contain a reference to Rectangle objects.
o Refer to Figure 4.3 to see the illustration of reference variable after the object is created in memory.
[You must be registered and logged in to see this image.]
Figure 4.3: States of the object (rectOne) after data has been stored in it
Note:
o An object is an instance of a class. You can create many instances of a class. Creating an instance is referred to as instantiation. The terms object and instance are often interchangeable.
o The relationship between classes and objects is analogous to the relationship between nasi lemak recipes and nasi lemak. You can make as many nasi lemak as you want from a single nasi lemak recipe.
Accessing an Object’s Data and Methods
After an object is created, its data can be accessed and its method invoked using the following
syntax: (using dot operator)
• Data/member: (if member is public)
objectname.data
• Method:
objectname.method
A Demo Program
Since Program 1 is not an application program (but it is a blueprint to create Rectangle objects), other programs will use the Rectangle class to create rectangles. The programs that create and use these objects will have their own main methods. Therefore, we need a demo program to test our class program.
Program 2 demonstrates some of the methods of the Rectangle class.
[You must be registered and logged in to see this image.]
Program 2: TestRectangle.java
Note:
• Program 2 must be saved in the same folder or directory as the file of Rectangle.java
• When the compiler reads the source code for TestRectangle.java and sees that a class named Rectangle is being used, it looks in the current folder/directory for the file Rectangle.class. (if it is not exists, compiler searches the file of Rectangle.java and compiles it)
CHECKPOINT
Q1 - What is the output of Program 2?
Q2 – What is the color of rectOne?
Q3 – How to write a statement to print rectTwo’s perimeter ?
Note:
• A main class (Rectangle) and a demo program (TestRectangle) can be combined in one file.
• BUT only one class can be public class. Public class must have the same name as the file name with the main method.
• Refer to Program 3. Check out the code at line 11.
[You must be registered and logged in to see this image.]
Program 3: Combine a class program and demo program
Modifiers
Java provides several modifiers that control access to data, methods and classes. This section
introduces the public, private and default modifiers.
• public
- defines classes, methods, and data in such way that all programs can access them.
• private
- defines classes, methods, and data in such way that they can be accessed by the declaring class but not by any other class.
• If public and private is not used, then by default the classes, methods and data are accessible by any class in the same package. This is known as package-private or package-access.
Refer the Figure 4.4 to understand this concepts
[You must be registered and logged in to see this image.]
• The private modifier restricts access within a class, the default modifier restricts access within a package, and the public modifier enables unrestricted access.
An object can access its private members if it declared in its own class. Refer to
Figure 4.6(a) & (b).
[You must be registered and logged in to see this image.]
Note:
(a) : This is OK because object s is used inside the ABC class
(b) : This is wrong because x and add are private in ABC
LifeStyle- Beginner
- Posts : 10
Points : 75870
Reputation : 0
Join date : 2014-01-06
Location : Behide you
Similar topics
» C4 OBJECT – ORIENTED PROGRAMMING ( Part 1 )
» 函数对象function object
» Intro to C++ programming
» What programming language should i learn?
» Best websites to learn Programming
» 函数对象function object
» Intro to C++ programming
» What programming language should i learn?
» Best websites to learn Programming
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|