Skip to main content

Interface Chapter No 6 Object Oriented Programming with Java DAE CIT Java Notes

 

Interfaces

6.1. Interface

An interface in Java is a collection of abstract methods (methods without a body) and constant declarations. It provides a way to achieve abstraction in Java, allowing the definition of a set of methods that must be implemented by any class that implements the interface.


Syntax:

interface MyInterface {

    // Declare constant(s)

    int MY_CONSTANT = 42;


    // Declare abstract method(s)

    void myMethod();

}

Explanation:

Interfaces declare methods without providing a method body. They only specify what methods a class should implement.

Constants in interfaces are implicitly public, static, and final.

Any class implementing an interface must provide concrete implementations for all the declared methods.

Programming Example:

interface MyInterface {

    int MY_CONSTANT = 42;

    void myMethod();

}

class MyClass implements MyInterface {

    @Override

    public void myMethod() {

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

    }

}

public class Main {

    public static void main(String[] args) {

        MyClass obj = new MyClass();

        obj.myMethod();

        System.out.println("Constant value: " + MyInterface.MY_CONSTANT);

    }

}

6.2. Extend Interfaces

Define:

In Java, an interface can extend another interface. This allows the new interface to inherit the abstract methods and constants of the extended interface.

Syntax:

interface MyInterface {

    void myMethod();

}


interface MyExtendedInterface extends MyInterface {

    void myExtendedMethod();

}

Explanation:

MyExtendedInterface inherits the abstract method myMethod from MyInterface.

Classes implementing MyExtendedInterface must provide concrete implementations for both myMethod and myExtendedMethod.

Programming Example:

interface MyInterface {

    void myMethod();

}


interface MyExtendedInterface extends MyInterface {

    void myExtendedMethod();

}


class MyClass implements MyExtendedInterface {

    @Override

    public void myMethod() {

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

    }


    @Override

    public void myExtendedMethod() {

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

    }

}


public class Main {

    public static void main(String[] args) {

        MyClass obj = new MyClass();

        obj.myMethod();

        obj.myExtendedMethod();

    }

}

6.3. Implement an Interface

Define:

To use an interface in a class, you need to implement the interface using the implements keyword. This means providing concrete implementations for all the abstract methods declared in the interface.


Syntax:

interface MyInterface {

    void myMethod();

}

class MyClass implements MyInterface {

    @Override

    public void myMethod() {

        // Implementation of myMethod

    }

}

Explanation:

The implements keyword is used to indicate that a class is going to provide concrete implementations for the methods declared in the interface.

Programming Example:

interface MyInterface {

    void myMethod();

}


class MyClass implements MyInterface {

    @Override

    public void myMethod() {

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

    }

}


public class Main {

    public static void main(String[] args) {

        MyClass obj = new MyClass();

        obj.myMethod();

    }

}

6.4. Implement Multiple Interfaces

Define:

A class in Java can implement multiple interfaces. This allows a class to inherit and provide implementations for the abstract methods declared in multiple interfaces.


Syntax:

interface Interface1 {

    void method1();

}


interface Interface2 {

    void method2();

}


class MyClass implements Interface1, Interface2 {

    @Override

    public void method1() {

        // Implementation of method1

    }


    @Override

    public void method2() {

        // Implementation of method2

    }

}

Explanation:

The class MyClass must provide concrete implementations for all methods declared in both Interface1 and Interface2.

Programming Example: 

interface Interface1 {

    void method1();

}

interface Interface2 {

    void method2();

}


class MyClass implements Interface1, Interface2 {

    @Override

    public void method1() {

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

    }


    @Override

    public void method2() {

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

    }

}


public class Main {

    public static void main(String[] args) {

        MyClass obj = new MyClass();

        obj.method1();

        obj.method2();

    }

}

6.5. Differentiate between Interface and Abstract Class

Define:

An abstract class in Java is a class that may have abstract methods (methods without a body) as well as concrete methods (methods with a body). An interface, on the other hand, is a collection of abstract methods and constants.


Explanation:

An abstract class can have fields (variables), constructors, and methods with or without a body.

An interface can only have constants and abstract methods, and all the methods are implicitly public and abstract.

A class can implement multiple interfaces, but it can extend only one abstract class.

Programming Example:

// Abstract Class

abstract class MyAbstractClass {

    int myField;


    MyAbstractClass(int value) {

        this.myField = value;

    }


    abstract void myAbstractMethod();


    void myConcreteMethod() {

        System.out.println("Concrete method in abstract class");

    }

}


// Interface

interface MyInterface {

    int MY_CONSTANT = 42;


    void myMethod();

}


// Class implementing an Interface and extending an Abstract Class

class MyClass extends MyAbstractClass implements MyInterface {

    MyClass(int value) {

        super(value);

    }


    @Override

    void myAbstractMethod() {

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

    }


    @Override

    public void myMethod() {

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

    }

}


public class Main {

    public static void main(String[] args) {

        MyClass obj = new MyClass(10);

        obj.myAbstractMethod();

        obj.myMethod();

        obj.myConcreteMethod();

        System.out.println("

constant value: " + MyInterface.MY_CONSTANT);

    }

}

In this example, MyClass implements MyInterface and extends MyAbstractClass, demonstrating the combination of abstract classes and interfaces in a class hierarchy.





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