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

Short Q&A about constructors

  Q1: What is a constructor in Java, and when is it called? A1: A constructor in Java is a special method used to initialize objects. It is called when an instance of a class is created, allocating memory for the object in RAM. Q2: What happens if a class in Java does not have any constructor defined? A2: If a class in Java doesn't have any constructor defined, the Java compiler provides a default constructor by default. Q3: What are the two types of constructors in Java? A3: There are two types of constructors in Java:  1. No-arg constructor (Default constructor) 2. Parameterized constructor Q4: What are the rules for creating a Java constructor? A4: The rules for creating a Java constructor are as follows: - Constructor name must be the same as its class name. - A constructor must have no explicit return type. - A Java constructor cannot be abstract, static, final, or synchronized. Q5: What are the different access modifiers that can be used while declaring a constructor in ...

Mgm 211 Business communication CIT 2ndyear Course outline

 

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...