PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 4, 2022

[FIXED] How Inheritance works in Java with method reference operator

 November 04, 2022     inheritance, java, lambda, method-reference     No comments   

Issue

Interface

public interface InterfaceOne {
    void start();
    void stop();
}

Main Class

public class MainProg {
    public static void main(String [] args){
        new ServiceA().start();
    }
}

ServiceA

import java.util.ArrayList;
import java.util.List;

public class ServiceA implements InterfaceOne{
    private final List<ServiceB> worker = new ArrayList<>();
    public ServiceA(){
        for(int i = 0; i < 2; i++){
            worker.add(new ServiceB(2));
        }
    }

    @Override
    public void start() {
        worker.forEach(InterfaceOne::start);
    }

    @Override
    public void stop() {
        worker.forEach(InterfaceOne::stop);
    }
}

ServiceB

public class ServiceB extends ServiceC{
    int count;
    protected ServiceB(int num){
        this.count = num;
    }
}

ServiceC

public class ServiceC implements InterfaceOne{
    @Override
    public void start() {
        System.out.println("Starting..");
    }

    @Override
    public void stop() {
        System.out.println("Stopping..");
    }
}

Here from the main class, I am calling a method of ServiceA that internally calls to the method of serviceB using the method reference operator. ServiceA Can be also written like below where instead of using the method reference operator i can use lambda function

import java.util.ArrayList;
import java.util.List;

public class ServiceA implements InterfaceOne{
    private final List<ServiceB> worker = new ArrayList<>();
    public ServiceA(){
        for(int i = 0; i < 2; i++){
            worker.add(new ServiceB(2));
        }
    }

    @Override
    public void start() {
        worker.forEach(obj -> obj.start());
    }

    @Override
    public void stop() {
        worker.forEach(obj -> obj.stop());
    }
}

Here I am aware of how this program is working using lambda function, but want to understand how it is working with the method reference operator

worker.forEach(InterfaceOne::start);

The output of this program is

Starting..
Starting..

Solution

You wrote:

worker.forEach(InterfaceOne::start);

This calls the start method for every element of the worker list. It does not matter if it's called in the scope of a method of ServiceA, the elements of the worker list are ServiceB's, which inherit the start method of ServiceC.



Answered By - GeertPt
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing