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

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

Object Oriented Programming Using Java-14 EN


Polymorphism, Nested Classes & Static Class Members

Polymorphism: It is the principle where an object belongs to different classes.

This happens when the super class is abstract, 
meaning that the super class contains at least one abstract method. This case, there are usually more than one class extend this abstract class. 

Known that you cannot create an object from an abstract class. Therefore, polymorphism exists.

The general syntax to declare an object using polymorphism:

super_class object_name=new sub_class (parameter_list);


Where:

super_class: it is the name of the super abstract class
object_name: it is the name of the created object
sub_class: it is the name of the sub class
parameter_list: it is the passed arguments which allows the compiler to choose the suitable constructor from those inside the sub class

The created object must be an object from the sub class because the super class is abstract in case of polymorphism.

Consider the following example which illustrates the principle of polymorphism:

Example (1):

The class
Vehicle:



The class
Car:



The class
Truck:



The class
MotorCycle:



The class
MyClass:



The output box:



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


Nested Classes: It is the definition of a class inside the body of some other class.

This way, the inner class can use both its code & the code of the outer class.

This seems like inheritance, where some class extends some other class, but we don't have 
extends keyword here. So, the creation of an object from the inner class won't make a call to the default constructor of the outer class.

Consider the following example which illustrates the principle of nested classes:

Example (2):

The class
Class1:



The class
MyClass:



The output box:


Note: No need to define the instance variables of the outer class as protected or public to make them accessible by the inner class because this is not inheritance.

Note: You cannot create an object from the inner class outside the scope of the outer class.
So, main cannot get access directly to Class2.

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

Static Data Members: They are the class data members (instance variables & methods) which are declared with the static reserved keyword.

The static data members exist and are usable even if an object from the class isn't created, meaning that you can access them directly through their class name without the need to create an object.

We know that every object is a distinct copy from its class, but static data members are common for all objects, meaning that the modification of some static instance variable inside an object will affect its value in all other objects.

But there are some limitations for using static data members:


1- Static methods can only access static data members (
instance variables & methods).Whereas, non-static methods can access both static and non-static data members.

2- Static methods cannot use this or super reserved keywords.

3- Static instance variables are "single copy variables", meaning that there exists only one copy of static instance variables for all objects.

The following two examples illustrates the static data members:

Example (3):

The class
Class1:



The class
Class1Test:



The output box:



Note that the variable y is common for all objects.

The method print is also static. So, it cannot use anything other than static data members.

Example (4):

The class
Class1:



The class
Class2:



The class
MyClass:



The output box:



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

To get text copies from the last 4 examples:

Example (1)
Example (2)
Example (3)
Example (4)

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

Arrays, ArrayList & Strings are to be discussed next lesson ...

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

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

إرسال تعليق