الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

does the interface hasa constructor

مغلق
بدأه dantelllah في 2 يناير 2007 · 10 رد · 1,010 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

salaaam...

i wanna ask if i've already class which is inherited from many other classes

and i want to change this class to be interface to the inherited classes, so they can implement it..

mt quistion is ...what about the super class constructor..what should i do to it..

because as i know the interface does not have any implementation for any method

تحيـــــــــــــــاتي..

الرجاء الكتابة باللغة العربية .. طالما انه لا يوجد ظرف يمنعك بالكتابة بالعربية مثل عدم توافر لوحة مفاتيح عربية

تم تعديل هذه المشاركة بواسطة nardien28 في 4 يناير 2007 في 15:05

What a Wonderful World

#2

:o

hmmmmm no body answer my question which means

something form both

:s my quistion is too hard that no one could answer it

or

-_- it's too easy that no one want to wast his/her time in answering those kind of question

which one do u think!!

What a Wonderful World

#3

السلام عليكم

ممكن السبب من وراء تحويل ال class الى interface

لنفرض ان لديك الكلاس التالية

class OldClass
{  protected String lpItem=""; 
   OldClass(){}
   OldClass(String lpItem){this.lpItem=lpItem;}
   public void setItem(String newItem)
	{lpItem=newItem;}
   public String getItem()
	{return lpItem;}	
}

هل هذه الطريقة تلبي طلبك

interface NewClassInterface
{
  abstract void setItem(String newItem);
  abstract String getItem();
};

abstract class NewClass implements NewClassInterface
{ protected String lpItem="";
  NewClass (){this("");}
  NewClass(String lpItem){this.lpItem=lpItem;}
  /**the next methods are implemented methods from NewClassInterface
   * and also must be (implemented) overrided in the class that inherit this class
  */
  abstract public void setItem(String lpNewItem);
  abstract public String getItem();
}

public class ClassName extends NewClass  
{
  ClassName(){this("");}
  ClassName(String lpItem){setItem(lpItem);}
  /* setItem and getItem must be overrided
   * and implemented like if it was implemented from an interface
   * i hope that what u looking for
   */
   public void setItem(String newItem)
	{super.lpItem=newItem;}
   public String getItem()
	{return super.lpItem;}

   public static void main (String[] args)
	{
	 ClassName c=new ClassName("Alaa");
	 System.out.println(c.getItem());
	}
}

هل هذا يفيدك بشئ؟

والله أعلم

سبحانك اللهم وبحمدك أشهد ان لا اله الا أنت

أستغفرك وأتوب اليك

الهم صلي وسلم وبارك على سيدنا وحبيبنا محمد

#4

مشكوور محب الرسول ..

am trying to open my miiiiiiiiiind as much as i can to undrestand.. :blink:

but what if i already have an abstract class and other classes inherited this class

can i change this super class to be an interface class, and make the other classes implement it..

thxxxxxxxxxx

تحيـــــــــــــــــــاتي

تم تعديل هذه المشاركة بواسطة dantelllah في 3 يناير 2007 في 11:11

What a Wonderful World

#5

السلام عليكم

اقتباس
can i change this super class to be an interface class, and make the other classes implement it
yes you can

ولكن اذا كان هناك constructor for the intefraceاو interface intilizer يجب التخلص منهم او تبرعي فيهم :D

ال intilizer الذي اقصدة هو كالتالي

class Class_Has_Static_Intilizer
{   public static String strName="";
 //using static will cause to execute the inclosed code on the first use  
 static
 {
  System.out.println("Class_Has_Static_Intilizer" );
 } 

}

public class IntilizerClass
{
 public static String strName="variable before calling the Instance Class Intilizer";

 {//this code will be executed when new instance of the class been created
  strName="variable has changed after initializing the instance";
  System.out.println("Instance Class Intilizer in IntilizerClass");
 }

 public static void main (String[] args)throws Exception
 {
  //this will cause to execute the Class_Has_Static_Intilizer static intilizer
  Class_Has_Static_Intilizer.strName="";

  //the Class_Has_Static_Intilizer static intilizer will not be executed
  new Class_Has_Static_Intilizer();

  System.out.println(strName );

  //this will cause to execute the IntilizerClass instance intilizer when create the instance
  System.out.println(new IntilizerClass().strName );
  System.in.read();

 }
}

هل هذا يساعد؟

سبحانك اللهم وبحمدك أشهد ان لا اله الا أنت

أستغفرك وأتوب اليك

الهم صلي وسلم وبارك على سيدنا وحبيبنا محمد

#6

يعطيك العافية اخوي...

اكتشفت شيء..يمكن هو موجود من اول..بس بالنسبة لي جديد :D

as i told u i had an abstract class and some classes inherit this class

- because there are 2 methods in the super class and this class supposes to not implement them..soo

-i made an interface that contains the 2 methods and off course with no definition.."implementation"

- i kept the class abstract but it implements that interface , and because it's abstract the 2 methods becomes abstract also..but i implement the constructor

- so the other classes still the same inherit the same super class

i think it's a good solution because i got the right result

what do u think!!

What a Wonderful World

#7

يعطيك العافية اخوي...

اكتشفت شيء..يمكن هو موجود من اول..بس بالنسبة لي جديد :D

as i told u i had an abstract class and some classes inherit this class

- because there are 2 methods in the super class and this class supposes to not implement them..soo

-i made an interface that contains the 2 methods and off course with no definition.."implementation"

- i kept the class abstract but it implements that interface , and because it's abstract the 2 methods becomes abstract also..but i implement the constructor

- so the other classes still the same inherit the same super class

i think it's a good solution because i got the right result

what do u think!!

What a Wonderful World

#8

يعطيك العافية اخوي...

اكتشفت شيء..يمكن هو موجود من اول..بس بالنسبة لي جديد :D

as i told u i had an abstract class and some classes inherit this class

- because there are 2 methods in the super class and this class supposes to not implement them..soo

-i made an interface that contains the 2 methods and off course with no definition.."implementation"

- i kept the class abstract but it implements that interface , and because it's abstract the 2 methods becomes abstract also..but i implemented the constructor

- so the other classes still the same inherit the same super class

i think it's a good solution because i got the right result

what do u think!!

What a Wonderful World

#9

السلام عليكم

الله يعافيكي

حسب ما فهمت هل هدفك كان انه ال tow methods ما بدك تستخدميهم في كل الكلاسات التي ترش ال super class؟

اقتباس
because there are 2 methods in the super class and this class supposes to not implement them..
تستطيعي بان تزيلي ال abstract من تعريف المثود فلا يشترط overrid and implement them والله أعلم

ممكن تضعي مثال بسيط حتى تتضح الفكرة أكثر

سبحانك اللهم وبحمدك أشهد ان لا اله الا أنت

أستغفرك وأتوب اليك

الهم صلي وسلم وبارك على سيدنا وحبيبنا محمد

#10

هي الsuper كلا س كانت Shape

وفيها two methods that the other classes like Circle and Rectangel should implement them

1- area()

2- moveto()

so i wrote this code

abstract class Shape implements shapeinterface{

////some methods with implemntation

abstract void area();
abstract double moveto();

interface shape_interface{
void area();
double moveto();

}

////then i made the other classes like Circle and Rectangle just inherit the Shape class
class Rectangle extends Shape{
void area(){
return width * height:
} 
}

هي مشية معي..لكن انا كنت بأل عن الكلا س Shape لأني كنت احسب بالأول أني لازم ما اعمل implementation for it's constructor

لكن بهذي الطريقة قدرت انفذ ال داخلهاconstructor

ان شاء الله اكون قدرت أوصل النقطة اللي مني متاكدة منها..

تحيــــــــــــاتي

تم تعديل هذه المشاركة بواسطة dantelllah في 4 يناير 2007 في 22:51

What a Wonderful World

#11

السلام عليكم

اقتباس
ان شاء الله اكون قدرت أوصل النقطة اللي مني متاكدة منها..
unfortunately no :(

يعني انتي عندك methods وبدك الكلاس implements كل الميثود ولكن في نفس الوقت تريدي يكون لل superclass constructor ؟

على اية حال

كل الميثود الموجودة في ل interface يجب ان implemented من الكلاس التي imlplemt هذا ال interface

الى في حالة ان ال class تكون abstract فلا يشترط ان الكلاس implemts كل الميثود الموجودة في ال interface

ولكن عند وراثة ال abstract من كلاس أخري يجب ان يتم implement الميثود المتبقية

في مثالك الموجود باأعلى الأفضل الأستغناء عن ال abstract class Shape وان تعملي implemts لل shape_interface من Rectangle على طول

الى أذا اردت ان تضيفي constructor لل Shape او ان تضعي فيها ميثود وبها كود يتم وراثها من جميع الكلاسات او اردت ان لا تشترطي implements أحد الموجودة في الinterface مثل الميثود area مثلا تكتبي لها body داخل الكلاس shape ولا تعرفيها على اساس انها abstract فعند اذن يشترط فقط implement الميثود moveto

اما الميثود area فيتم وراثتها من جميع الكلاسات مع امكانية override it

اقتباس
هي مشية معي..لكن انا كنت بأل عن الكلا س Shape لأني كنت احسب بالأول أني لازم ما اعمل implementation for it's constructor
نعم يجب ان يكون هناك على الأقل constructor واحد يقابل ذلك الموجود في ال Shape وفي المثال السابق لأننا لم نحدد constructor سينشئ constructor تلقائي كهاذا
Rectangle(){super();}

والله أعلم

تستطيعي اضافة constructor ال الكلاس shape مثل

Shape()
Shape(int width,int height)

يجب ان يتم احتواء الكلاس Rectangle على واحدة على الأقل مقابلة

طبعا قلنا ان ال constructor Rectangle(){super();} موجود bydefault في حالة عدم وجود constructor

اما اذا أزلنا الconstructor

Shape()

فيجب انشاء constructor مماثل لل

Shape(int width,int height){}

يكون كالتالي

Rectangle(int width,int height) super(width,height);
..... optional code

والله أعلم طبعا

أرجو ان يفيدك

تم تعديل هذه المشاركة بواسطة Mo7eb_Alrasool في 7 يناير 2007 في 10:44

سبحانك اللهم وبحمدك أشهد ان لا اله الا أنت

أستغفرك وأتوب اليك

الهم صلي وسلم وبارك على سيدنا وحبيبنا محمد

هذا الموضوع مغلق.

مواضيع مشابهة