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

 

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