📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Below diagram describe that the methods return int value:
return Java Keyword Example
class AddOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 + num2);
}
}
class SubOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 - num2);
}
}
class MultiplyOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 * num2);
}
}
class DivisionOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 / num2);
}
}
Complete Example
package com.javaguides.corejava.keywords;
public class ReturnKeyword {
public static void main(String[] args) {
Operations addOperation = new AddOperation();
Operations subOperation = new SubOperation();
Operations mulOperation = new MultiplyOperation();
Operations divOperation = new DivisionOperation();
int add = addOperation.doOperation(10, 20);
System.out.println("Addition of 10 and 20 ::" + add);
int sub = subOperation.doOperation(20, 10);
System.out.println("Substraction between 20 and 10 :: " + sub);
int mul = mulOperation.doOperation(10, 20);
System.out.println("Multiply 10 and 20 :: " + mul);
int div = divOperation.doOperation(20, 10);
System.out.println("Division of 20 by 10 :: " + div);
}
}
interface Operations {
public int doOperation(int num1, int num2);
}
class AddOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 + num2);
}
}
class SubOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 - num2);
}
}
class MultiplyOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 * num2);
}
}
class DivisionOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 / num2);
}
}
Addition of 10 and 20 ::30
Substraction between 20 and 10 :: 10
Multiply 10 and 20 :: 200
Division of 20 by 10 :: 2
Comments
Post a Comment
Leave Comment