Friday 14 January 2022

Question 8: State four differences between interfaces and classes with suitable examples. उपयुक्त उदाहरणों के साथ इंटरफेस और कक्षाओं के बीच चार अंतर बताएं।


 

Class

Interface

A class describes the attributes and behaviors of an object.

एक क्‍लास (वर्ग) किसी ऑब्‍जेक्‍ट (वस्तु) के गुणों और व्यवहारों का वर्णन करता है।

An interface contains behaviors that a class implements.

एक इंटरफ़ेस में ऐसे व्यवहार होते हैं जो एक क्‍लास(वर्ग) लागू करता/करती है।

A class may contain abstract methods, concrete methods.

एक वर्ग में अमूर्त विधियाँ, ठोस विधियाँ हो सकती हैं।

An interface contains only abstract methods.

एक इंटरफ़ेस में केवल अमूर्त विधियाँ होती हैं।

Members of a class can be public, private, protected or default.

एक वर्ग के सदस्य सार्वजनिक, निजी, संरक्षित या डिफ़ॉल्ट हो सकते हैं।

All the members of the interface are public by default.

इंटरफ़ेस के सभी सदस्य डिफ़ॉल्ट रूप से सार्वजनिक होते हैं।

The keyword used to create a class is “class”

क्लास बनाने के लिए इस्तेमाल किया जाने वाला कीवर्ड "क्लास" है

The keyword used to create an interface is “interface”

इंटरफ़ेस बनाने के लिए प्रयुक्त कीवर्ड "इंटरफ़ेस" है

Syntex:-

 
class Box {
public:
double length;   // Length of a box
double breadth;  // Breadth of a box
double height;   // Height of a box
};

 

Example:

 

class Goods

{

   String description;

   double price;

   Goods( String des, double pr )

   {

    description = des;

     price = pr;

   }

   void display()

   {

   System.out.println( “item: “ + description + “ price: “ + price );

   }

}

Syntex:-

 

interface InterfaceName

{

constant definitions

method declarations (without implementations.)

}

 

Example:

interface MyInterface

{

public final int aConstant = 32; // a constant

public final double pi = 3.14159; // a constant

public void methodA( int x ); // a method declaration

double methodB(); // a method declaration

}


No comments:

Post a Comment