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

 

Chapter No 2 Java DAE CIT

  . Variable and Operators 1.1. Variable: Define: A variable is a named storage location in a program that holds data, and its value can be changed during the execution of the program. Syntax: datatype variableName; Explanation: Variables are used to store and manipulate data in a program. They have a data type that defines the kind of data they can hold, such as int, float, char, etc. Code int age; // Declaration of an integer variable age = 25; // Initialization of the variable with a value 1.2. Create Instance of a Variable: Define: Creating an instance of a variable involves declaring and optionally initializing a variable in a program. Syntax: datatype variableName = value; Explanation: This creates a variable and assigns an initial value to it at the time of declaration. score = 95.5  # Declaration and initialization of a floating-point variable 1.3. Use Preemptive Data Types: Define: Preemptive data types are data types that are predefined and provided by...

Chemistry Past papers of DAE CIT 1st year

1. **What are Derived units? Give examples.**    - Derived units are units of measurement that are derived from the base units of a system. They express quantities in terms of fundamental units. Examples include:      - Newton (N) for force (kg·m/s²)      - Joule (J) for energy (N·m)      - Watt (W) for power (J/s) 2. **Define Radical, Valency, Formula, and Chemical Equation.**    - Radical: A radical is a group of atoms bonded together that behaves as a single unit in chemical reactions.    - Valency: Valency is the number of chemical bonds an atom can form when it combines with other atoms in a compound.    - Formula: A formula is a representation of a chemical compound using symbols and subscripts to show the ratio of atoms in the compound.    - Chemical Equation: A chemical equation is a symbolic representation of a chemical reaction, showing the reactants and products with their respectiv...