Skip to main content

Chapter 9 Interface and Abstract Classes in Java - Technology369kk

In this chapter, we’ll explore two core concepts of Java's object-oriented programming: Interfaces and Abstract Classes. Both play a crucial role in achieving abstraction and defining a contract for classes, so i hope this chapter is most important for Java Learner how to implement and execute now let's get start.

Introduction Java Interface : 

In Java, an interface is a fundamental concept that allows you to define a contract or a set of methods that a class must implement. An interface defines the method signature but doesn't provide any methods implement. It acts as a blueprint for classes that implement it, ensuring that these classes provide specific functionality.  

According the CODE WITH HARRY : 
Example1:  For Example A Coder write a code with the help of Keywords then visible code on VSC or system/ Computer. 
Example2: Your Phone, ( Camera, Sound, Audio Recorder, Telephones etc. )  
 
There are interface is a Keywords, why? bcz it is interact between USER and COMPUTER so. 

Let's discuss in Java>  In java interface is a group of relate methods with empty bodies. 

Declaration  Interfaces: 

Interface provide a way to achieve abstraction and define a set  of signature that classes implementing the interface must adhere to. To declare an interface in java, you use the interface keywords 

interface myInterface{
    // Methods Declaration(Abastract method)
    void method1();
    int method2(String str);
    ......................

    Constant declclarations (implicity, public, static and final keywords )
    int myValue = 369 ;
}

Explanation  of Code: 

  • The interface keyword is used to declare an interface.
  • Inside the interface, you can declare methods signature, but these methods are implicitly public and abstract. you don't need to use the public and abstract modifiers explicitly.
  • In java all fields(variables) declared within an interface are implicitly(public, static and  final) This filed are effectively by implementing classes.  

Here is a An Example of Interface Bicycle 

Q1. Bicycle all Features..

import java.util.*;

interface Bicycle {
    int a = 45;  // Public, static, and final by default
    void applyBrake(int decrement);
    void speedup(int increment);
}

class AvonCycle implements Bicycle {
    void blowHorn() {
        System.out.println("Pee Poo Po-Up");
    }

    public void applyBrake(int decrement) {
        System.out.println("Applying Brake with decrement: " + decrement);
    }

    public void speedup(int increment) {
        System.out.println("Speeding up with increment: " + increment);
    }
}

class C9_interface{
    public static void main(String args[]) {
        AvonCycle cycleShailu = new AvonCycle();
        cycleShailu.applyBrake(1);  // Corrected call
        cycleShailu.speedup(5);     // Added call to speedup
        cycleShailu.blowHorn();     // Added horn method call
       
        System.out.println("Value of a: " + Bicycle.a);  // Accessing interface variable
    }
}

// OUTPUT

// PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> java C9_interface
// Applying Brake with decrement: 1
// Speeding up with increment: 5
// Pee Poo Po-Up
// Value of a: 45

Another Example of Interface : 


 

Comments

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...