اتحاد طلبة هندسة الحاسوب والشبكات - المدونه الرسميه C.N.E : Object Oriented Programming Using Java-9

المشاركات الشائعة

Object Oriented Programming Using Java-9





Detailed Example About Constructors & Overloaded Methods

Consider the following program which covers both constructors & overloaded methods:

Class
Program:


Class
MyProgram:


Consider the following detailed illustration to understand how the program works & how the output is obtained.

Firstly, each object is a distinct copy of the class. So, to show how the instance variables values of each object change over the sequence of
main instructions, we are representing each objects by a rectangle for simplification and illustration.

Note that instructions in class
MyProgram are numbered from 1 to 16, these numbers are written to be used in illustration.

Instruction 1 creates object P1 from class Program using the default constructor (the 1st constructor) as the parameter list indicates.
Effects on objects:


Effects on output: (Nothing)

Instruction 2 creates object P2 from class Program using the 2nd constructor as the parameter list indicates.
Effects on objects:

Effects on output: (Nothing)

Instruction 3 creates object P3 from class Program using the 3rd constructor as the parameter list indicates.
Effects on objects:

Effects on output: (Nothing)

Instruction 4 prints the values of x and y in object P1.
Note that the instruction uses the dot operator to reach the instance variables of object P1 because they are declared as public.

Effects on objects:(Nothing)
Effects on output:

Instruction 5 prints the values of x and y in object P2.
Note that the instruction uses the dot operator to reach the instance variables of object P2 because they are declared as public.
Effects on objects:(Nothing)
Effects on output:

Instruction 6 prints the values of x and y in object P3.
Note that the instruction uses the dot operator to reach the instance variables of object P3 because they are declared as public.
Effects on objects:(Nothing)
Effects on output:

Instruction 7 creates a new line at the output screen.
Effects on objects:(Nothing)
Effects on output:

Instruction 8 prints the values of x and y in object P1Note that the compiler will choose the 1st print method in class Program
Effects on objects:(Nothing)
Effects on output:

Instruction 9 prints the values of x and y in object P2Note that the compiler will choose the 2nd print method in class Program which adds the passed value to the value of x before printing to the output screen.
Effects on objects:

Effects on output:

Instruction 10 prints the values of x and y in object P2Note that the compiler will choose the 2nd print method in class Program which adds the passed value to the value of x before printing to the output screen.
Effects on objects:

Effects on output:


Instruction 11 prints the values of x and y in object P3Note that the compiler will choose the 3rd print method in class Program which adds the passed values to both the value of x and the value of y before printing to the output screen.
Effects on objects:

Effects on output:

Instruction 12 prints the values of x and y in object P3Note that the compiler will choose the 3rd print method in class Program which adds the passed values to both the value of x and the value of y before printing to the output screen.
Effects on objects:

Effects on output:

Instruction 13 creates a new line at the output screen.
Effects on objects:(Nothing)
Effects on output:


Instruction 14 prints the values of x and y in object P1.
Note that the instruction uses the dot operator to reach the instance variables of object P1 because they are declared as public.

Effects on objects:(Nothing)
Effects on output:


Instruction 15 prints the values of x and y in object P2.
Note that the instruction uses the dot operator to reach the instance variables of object P2 because they are declared as public.
Effects on objects:(Nothing)
Effects on output:

Instruction 16 prints the values of x and y in object P3.
Note that the instruction uses the dot operator to reach the instance variables of object P3 because they are declared as public.
Effects on objects:(Nothing)
Effects on output:

Now, the main method terminates, so this is the output of the program:


To write the program using Eclipse, you should create a java project, then 2 .class files (To see how a project and a .class file are created, you can see
Preparing Your Computer For Java Programming).

You need 2 .class files because each class is to be written in a separated file but inside the same project.

When you create a .class file you should consider that the name of the .class file is the same as the name of class to be written inside the file.

If you want a soft copy of the program please download it from Constructors & Overloaded Methods. So you can copy & paste the code in Eclipse.

سامر المناصرة
< >

ليست هناك تعليقات:

إرسال تعليق