Skip to main content

Chapter No 6 Interface Java

 Chapter 6 

                   Interfaces 

6.1 Introduction 

6.2. Defining and Implementing Interfaces  

6.3. Advantages of using Interface 

 

6.1 Introduction 

An interface in Java is a collection of abstract methods. It provides a way to achieve abstraction and multiple inheritance in Java. An interface is a blueprint for a class and allows the definition of methods without specifying the implementation. Classes implementing an interface must provide concrete implementations for all its methods. 

 Syntax 

interface MyInterface{ { 

    // Declare methods (implicitly public and abstract) 

    void method1(); 

    void method2(); } 

 Explanation 

  • An interface is declared using the interface keyword. 

  • Methods declared in the interface are implicitly public and abstract. 

  • An interface may also contain constant (public, static, and final) variables. 

Programming Example 

interface Shape { 

    void draw(); // Abstract method 

    double calculateArea(); // Abstract method 

}  

// Implementation of the interface 

class Circle implements Shape { 

    double radius; 

    Circle(double radius) { 

        this.radius = radius; } 

    public void draw() { 

        System.out.println("Drawing Circle"); } 

 public double calculateArea() { 

        return Math.PI * radius * radius; 

    } } 

* radius * radius; } } 

6.2 Defining and Implementing Interfaces 

Defining interfaces involves specifying method signatures without providing implementations. Implementing interfaces requires concrete classes to provide actual implementations for the methods declared in the interface. 

 Syntax 

class MyClass implements MyInterface { 

    // Implement methods declared in MyInterface 

    @Override 

    public void method1() { 

        // Provide implementation 

    } 

    public void method2() { 

        // Provide implementation 

    } } 

//provide implementation } } 

Explanation 

  • Use the implements keyword to indicate that a class is implementing an interface. 

  • The implementing class must provide concrete implementations for all methods declared in the interface. 

 Programming Example 

interface MyInterface { 

    void method1(); 

    void method2(); 

// Class implementing the interface 

class MyClass implements MyInterface { 

    public void method1() { 

        System.out.println("Implementation of method1"); 

    }  

    public void method2() { 

        System.out.println("Implementation of method2"); 

    } } 

6.3 Advantages of using Interface 

Interfaces provide several advantages, including achieving abstraction, supporting multiple inheritance, and enabling code flexibility and reusability. 

 Explanation 

  • Abstraction: Interfaces allow you to define methods without specifying their implementations, promoting abstraction. 

  • Multiple Inheritance: Java supports multiple inheritance through interfaces, allowing a class to implement multiple interfaces. 

  • Flexibility and Reusability: Interfaces enhance code flexibility by enabling classes to implement multiple interfaces, promoting code reusability. 

 Programming Example 

interface A { 

    void methodA(); }  

interface B { 

    void methodB(); }

class MyClass implements A, B { 

    public void methodA() { 

        System.out.println("Implementation of methodA"); } 

 public void methodB() { 

        System.out.println("Implementation of methodB");}} 

 

 


Comments

Popular posts from this blog

DAE CIt 244 Electronics 2ndyear Past Paper 2019

 

Semantic Data Model Chapter No 3 RDBMS

  Chapter No 3 ⦁ Semantic Data Model ⦁ Relational Model ⦁ Database Models and Internet Semantic Data Model: "A semantic data model is a method of organizing data in a logical and meaningful way. " It provides a conceptual representation of data and the relationships between them, adding a layer of semantic information that gives data a basic meaning. Key Elements: 1.    - Entities: Represent objects or concepts (e.g., Person, Product). 2.    - Attributes: Characteristics or properties of entities. 3.    - Relationships: Connections between entities, defining associations through the foreign key. A semantic data model describes data about its real-world interpretation and usage.  For example, the object "Person" can be generalized to include "Employee," "Applicant," and "Customer," and is related to "Project" and "Task." A person can own multiple projects, and a specific task can be associated with differe...

Chapter No 2 Database System DAE CIT 2nd year

Chapter No. 2  Database System Legacy DB Systems    -  File Processing Systems    - Hierarchical Model    - Network Model Legacy Database Systems Legacy database systems played a crucial role in the evolution of data management, paving the way for more advanced relational database systems. Legacy Database Systems, including File Processing Systems Hierarchical Model Network Model,. 1. File Processing Systems Definition: File Processing Systems are traditional data management systems that use flat files to store and manage data. In this approach, each department or user group maintains its own set of files, leading to data redundancy. Characteristics: Data stored in flat files. No centralized control or structure. Limited data sharing and integration. Advantages: Simplicity: Easy to implement and understand. Independence: Each department has control over its data. Cost: Relatively low implementation and maintenance costs. Performance: Can be effic...