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

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

Object Oriented Programming Using Java-12



Interfaces, Implementation & an Example About Protected Access Modifier

Example: The following program is the same as the previous one, but we are using protected instead of private for the instance variables inside the super class.

The class
MyClass
:



The class
Class1:



The class
Class2:



The output box is:



Note that it is the same as the example explained last lesson, but we are introducing some differences...

We declared the instance variables in the super class as protected, so their scope extended also to the sub class, meaning that they are visible for both Class1 & Class2.
Therefore, we removed the declaration of sum from the sub class Class2.

We also put statements for print in both the constructors of super & sub classes to show how the precedence of calling constructors occurs when an object of the sub class is created.
We said that the default constructor of the super class is called before the constructor of the sub class, that's what happened when the constructor of Class1 is called before the constructor of Class2 when an object from Class2 is created.


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

Abstract Class: It is the class which contains at least one declared but undefined method.

The reserved keyword abstract is an access modifier for abstract classes.

The general syntax to declare an abstract class:

abstract class Class_Name
{ abstract return_type method_name (parameter_list); // Another java statements ... }

Where we may have more methods and instance variables, but we must have at least one abstract method for the abstract class.

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

Interface: It is the class which contains only abstract methods and final instance variables.

The
final reserved keyword is used to declare and initialize a variable instantly, which value cannot be changed thereafter.
Such as:
final int x=5;

The general syntax to declare an interface:

public interface Interface_Name
{ // Final instance variables & abstract methods only. }


Where interface is a reserved keyword used to declare a new interface. The interface as we discussed is a class contains only final instance variables and abstract methods.

To declare an interface in Eclipse you need to create a new interface:

1- creat a new project in java.
2- choose file, new, interface.
3- write the name of your interface, it must obey the variable name declaration rules.

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

Remember that you cannot extend more than one super class, but you can implement more than one interface.

The general syntax of implementation is:

public class Class_Name1 implements Interface_Name1, Interface_Name2, ..... , Interface_NameN
{ //Class_Name1 code & the definition of all the abstract methods in the implemented interfaces. }

Where the implementation for more than one interface occurs by writing a comma separated list of interfaces.
You need to define all the abstract methods of the implemented interfaces inside the sub class.

**************************************************
An example about abstract classes and interfaces is to be discussed next lesson ...
 

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

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

إرسال تعليق