Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

Patulong sa JAVA Programming tungkol sa INTERFACE

josephsanandres

Amateur
Advanced Member
Messages
141
Reaction score
0
Points
26
Di ko ma-gets ang interface programming ng java...

Bakit kailangan ng interface?

Saan ginagawa ang mga methods na declare sa interface class?

Parang redundancy ito sa inheritance...

Thanks sa makakasagot.
 
interface solves the diamond problem which is some kind of multiple inheritance issue, mas appropriate ang interface kasi kung gusto mo mag inherit sa magkakaibang classes.
 
Interface helps you decouple your code to concrete implementation implementation. The advantage of using interface is that you can easily swap out components of your code. Say for example you want to easily switch between different implementation of a Logger, you can have a FileLogger, ConsoleLogger, EmailLogger implements a common interface and make your code depend on that interface.



Read Stackoverflow1

Read Stackoverflow2
 
Last edited:
The two answers above are very good reasons of why interface exist.

In addition, Interface give you more flexibility in terms of polymorphism than just by inheritance alone.

To enforce the concept of interface, I'm providing code using the logger mechanism aQudei mentioned above. Study it carefully.

Code:
public class StrategyExample {

    public static void main(String[] args) {
        
        LogManager logManager = new LogManager();
        
        // Log to file...
        logManager.setLogger(new FileLogger());
        logManager.log("Writing to file...");
        
        // Log to console...
        logManager.setLogger(new ConsoleLogger());
        logManager.log("Writing to console...");
        
        // Log to email logger
        logManager.setLogger(new EmailLogger());
        logManager.log("Writing to email logger");
    }
}
Code:
public class LogManager {
    Logger logger;
    
    public LogManager() {}
    
    public void setLogger(Logger logger) {
        this.logger = logger;
    }
    
    public void log(String str) {
        logger.doLog(str);
    }
}
One and only interface
Code:
public interface Logger {
    public void doLog(String msg);
    // more method signature
}

All three classes below implement the Logger interface
Code:
public class FileLogger implements Logger {
    @Override
    public void doLog(String msg) {
        System.out.println("FileLogger: " + msg);
    }
    
    // .. other class methods
}
Code:
public class ConsoleLogger implements Logger {

    @Override
    public void doLog(String msg) {
        System.out.println("Console logger: " + msg);
    }
    
    // .. other class methods
}

Code:
public class EmailLogger implements Logger {

    @Override
    public void doLog(String msg) {
        System.out.println("Email logger: " + msg);
    }
    
    // .. other class methods
}
 
Meaning sa interface, puro declaration lang, then sa ibang class naggagaling mga methods. Tama po ba?
 
Meaning sa interface, puro declaration lang, then sa ibang class naggagaling mga methods. Tama po ba?

Yes, you can only declare methods in the Interface and they must be overriden by the implementing class(es). Interfaces also cannot be instantiated.
 
Back
Top Bottom