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

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

Object Oriented Programming Using Java-13 EN


An Example About Implementation of Interfaces & Multi-Inheritance

This example consists from three classes & three interfaces, where they are:

Interfaces:

Summation
Subtraction
Multiplication

Classes:

DivAndRem
BasicOperations
MyClass

Where:

The class BasicOperations inherits the three interfaces & the class DivAndRem.
The class MyClass contains the method main.

We can perform all of the above in the presence of only two classes, but we are using all of these classes & interfaces to illustrate the concepts of implementation & Multi-Inheritance.

The Interface Summation:




The Interface Subtraction:




The Interface Multiplication:




The Class DivAndRem:




The Class BasicOperations:




The Class MyClass:




The Output Box Is:




Note that you need to define the contents of all the abstract methods inside the class BasicOperations.
This case, avoid declaring a method inside some interface with the same name as another method declared inside some other interface. If you do, you will have to define two methods with the same name at class BasicOperations, which results in a compiler error if these methods cannot be dealt with as overloaded methods.

Note that the instance variables 
declared inside interfaces are final, which means that their values cannot be changed. So, when a variable is declared as a final, it must be initialized in the same statement of declaration. Otherwise, a compiler error occurs.

Don't panic! Too many classes & interfaces don't mean that this program is difficult to trace.
Just keep in mind that the executable code is located inside the method main, the place where you can declare objects, call methods, access instance variables & modify their values if possible.

Instructions written in main are executed serially & automatically when you run the program. Whereas, any instructions written outside the method main are unusable unless you call them with instructions in main!

The method main in our program contains three instructions, they are:

1- BasicOperations BO=new BasicOperations ();

This instruction declares a new object BO from the class BasicOperations, where a location in memory is reserved for this new copy of BasicOperations.

The default constructor of the super class DivAndRem is called, then the default constructor of the sub class BasicOperations is called.

Note that constructor are called only for classes because interfaces don't have constructors.
Only one super exists for a sub (You cannot have more than one father).
You can have many interfaces inherited by some class (You have many uncles for example).

2- BO.computeAll ();

This statement calls the method computeAll through the object BO.
computeAll calculates the results of the five basic operations and stores the results inside the five variables declared inside class BasicOperations
Methods of computing operations requires specifying which variable is accessed by writing the name of the related interface to avoid causing ambiguity for the compiler (For example, Subtraction.to access the variable y which is located inside the interface Subtraction).

3- BO.printAll ();

This statement calls the method printAll through the object BO.
printAll prints the values of x & y which are located in each of the inherited class & interfaces by calling methods specialized for that purpose. The methods of print which belong to interfaces require writing the name of the interface to access the variable, this is done to avoid ambiguity inside the class BasicOperations. (For example, Subtraction.y to access the variable y which is located inside the interface Subtraction).

The method printAll also prints the values of the five variables declared inside class BasicOperations, which is declared to store the results of the five operations.

**************************************************

To get a text version for this program, please press here.

Polymorphism, Nested Classes & Static Class Members are to be discussed next lesson ...


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

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

إرسال تعليق