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

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

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

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