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