Interfaces in Java can also extend other interfaces. This allows one interface to build on top of another and combine multiple behaviours. Things that became clear : • an interface can extend one or more interfaces • the new interface inherits all the method declarations from the parent interfaces • any class implementing the child interface must provide implementations for all inherited methods • this helps organize related behaviours into structured layers • it also supports building more complex systems while keeping interfaces modular A simple example helps illustrate this idea : interface ICalculator { void add(int a, int b); void sub(int a, int b); } interface IAdvancedCalculator extends ICalculator { void mul(int a, int b); void div(int a, int b); } class CalculatorImpl implements IAdvancedCalculator { public void add(int a, int b) { System.out.println(a + b); } public void sub(int a, int b) { System.out.println(a - b); } public void mul(int a, int b) { System.out.println(a * b); } public void div(int a, int b) { System.out.println(a / b); } } Through this structure, interfaces can be combined and expanded while still maintaining clear rules for the implementing classes. #java #oop #programming #learning #dsajourney
Lakhyadeep Sen’s Post
More Relevant Posts
-
Another way abstraction is implemented in Java is through interfaces. Interfaces define a set of methods that a class must implement, but they do not provide the actual implementation. Things that became clear : • an interface represents complete abstraction • methods declared inside an interface are implicitly public and abstract • a class uses the implements keyword to follow the rules defined by an interface • the implementing class must provide the body for all the methods • interfaces help create loose coupling between components A simple example shows the idea: interface ICalculator { void add(int a, int b); void sub(int a, int b); } class CalculatorImpl implements ICalculator { public void add(int a, int b) { System.out.println(a + b); } public void sub(int a, int b) { System.out.println(a - b); } } In this structure the interface defines what operations should exist, while the implementing class decides how those operations work. This approach makes it easier to design flexible and maintainable systems. #java #oop #programming #learning #dsajourney
To view or add a comment, sign in
-
The Diamond Problem in Java occurs in languages that allow multiple inheritance, where a class inherits from two classes that share a common superclass. This situation leads to ambiguity when both parent classes define the same method, raising the question of which method should be used. Java circumvents this issue by not supporting multiple inheritance with classes. However, a similar problem can arise with interfaces and default methods. Consider the following example: interface A { default void show() { System.out.println("A"); } } interface B { default void show() { System.out.println("B"); } } class Test implements A, B { public static void main(String[] args) { Test t = new Test(); t.show(); // This results in a compilation error } } In this case, since both interfaces provide the same method, Java requires the class to override it explicitly. Here’s how to resolve it with specific interface calls: class Test implements A, B { public void show() { A.super.show(); // Calls method from interface A B.super.show(); // Calls method from interface B } } This approach allows for explicit selection or combination of behavior from both interfaces. #Java #OOP #InterviewQuestions #BackendDevelopment #Programming
To view or add a comment, sign in
-
-
🚀 Java Series – Day 14 📌 Abstract Class vs Interface in Java 🔹 What is it? Both Abstract Classes and Interfaces are used to achieve abstraction in Java, but they are used in different scenarios. An Abstract Class can have: • Abstract methods (without body) • Concrete methods (with implementation) • Instance variables and constructors An Interface mainly contains: • Abstract methods (by default) • Constants (public static final) • Supports multiple inheritance 🔹 Why do we use it? We choose between them based on the requirement: • Use Abstract Class when classes share common code and state • Use Interface when we want to define a contract that multiple classes can implement For example: In a payment system, an abstract class can provide common logic like logging, while an interface defines methods like "pay()" for different payment types. 🔹 Example: abstract class Animal { void eat() { System.out.println("Animal eats food"); } abstract void sound(); } interface Pet { void play(); } class Dog extends Animal implements Pet { void sound() { System.out.println("Dog barks"); } public void play() { System.out.println("Dog plays"); } } public class Main { public static void main(String[] args) { Dog d = new Dog(); d.eat(); d.sound(); d.play(); } } 💡 Key Takeaway: Use abstract classes for shared behavior and interfaces for defining contracts and achieving multiple inheritance. What do you think about this? 👇 #Java #OOP #Abstraction #JavaDeveloper #Programming #BackendDevelopment
To view or add a comment, sign in
-
@programiz 🚀 Mini Java Project: Rock Paper Scissors Game As part of my continuous learning in Java development, I built a simple Rock Paper Scissors console game using Java. 🔹 Concepts used in this project: • Java Basics & Control Flow • Loops and Conditional Statements • Random class for computer choice • Scanner for user input • Switch expressions • Basic game logic implementation The program allows a user to play 3 rounds against the computer, and it tracks wins, losses, and draws before declaring the final result. Small projects like this help strengthen problem-solving skills and improve understanding of Java logic building #Java #Programming #Coding #SoftwareDevelopment #LearningJourney #JavaDeveloper
To view or add a comment, sign in
-
Q. Can an Interface Extend a Class in Java? This is a common confusion among developers and even I revisited this concept deeply today. - The answer is NO, an interface cannot extend a class. - It can only extend another interface. But there is something interesting: - Even though an interface doesn’t extend Object, all public methods of the Object class are implicitly available inside every interface. Methods like: • toString() • hashCode() • equals() These are treated as abstractly redeclared in every interface. ⚡ Why does Java do this? - To support upcasting and polymorphism, ensuring that any object referenced via an interface can still access these fundamental methods. ❗ Important Rule: While you can declare these methods in an interface, you cannot provide default implementations for them. interface Alpha { default String toString() { // ❌ Compile time error return "Hello"; } } Reason? Because these methods already have implementations in the Object class. Since every class implicitly extends Object, allowing default implementations of these methods in interfaces would create ambiguity during method resolution. Therefore, Java does not allow interfaces to provide default implementations for Object methods. 📌 Interfaces don’t extend Object, but its public methods are implicitly available. However, default implementations for them are not allowed. #Java #OOP #InterviewPreparation #Programming #Developers #Learning #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Java Series – Day 12 📌 Polymorphism in Java (Method Overloading vs Method Overriding) 🔹 What is it? Polymorphism is one of the four pillars of Object-Oriented Programming (OOP). The word polymorphism means “many forms.” In Java, polymorphism allows the same method name to perform different behaviors depending on the context. There are two main types: • Method Overloading – Same method name with different parameters in the same class. • Method Overriding – A child class provides a specific implementation of a method already defined in the parent class. 🔹 Why do we use it? Polymorphism improves flexibility and code reusability. For example: In a payment system, a method called "pay()" could work differently for CreditCard, UPI, or NetBanking, even though the method name is the same. 🔹 Example: class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { // Method Overriding void sound() { System.out.println("Dog barks"); } } public class Main { // Method Overloading static int add(int a, int b) { return a + b; } static int add(int a, int b, int c) { return a + b + c; } public static void main(String[] args) { Animal a = new Dog(); a.sound(); System.out.println(add(5, 10)); System.out.println(add(5, 10, 15)); } } 💡 Key Takeaway: Polymorphism allows the same method name to behave differently, improving flexibility and making Java programs more powerful. What do you think about this? 👇 #Java #OOP #Polymorphism #JavaDeveloper #Programming #BackendDevelopment
To view or add a comment, sign in
-
💎 Understanding the Diamond Problem in Java (and how Java solves it!) Ever heard of the Diamond Problem in Object-Oriented Programming? 🤔 It happens in multiple inheritance when a class inherits from two classes that both have the same method. The Problem Structure: Class A → has a method show() Class B extends A Class C extends A Class D extends B and C Now the confusion is: Which show() method should Class D inherit? This creates ambiguity — famously called the Diamond Problem Why Java avoids it? Java does NOT support multiple inheritance with classes. So this problem is avoided at the root itself. But what about Interfaces? Java allows multiple inheritance using interfaces, but resolves ambiguity smartly. If two interfaces have the same default method, the implementing class must override it. Example: interface A { default void show() { System.out.println("A"); } } interface B { default void show() { System.out.println("B"); } } class C implements A, B { public void show() { A.super.show(); // or B.super.show(); } } Key Takeaways: No multiple inheritance with classes in Java Multiple inheritance allowed via interfaces Ambiguity is resolved using method overriding Real Insight: Java doesn’t just avoid problems — it enforces clarity. #Java #OOP #Programming #SoftwareDevelopment #CodingInterview #TechConcepts
To view or add a comment, sign in
-
🚨 Stop confusing == 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿 with .𝗲𝗾𝘂𝗮𝗹𝘀() in Java! 🚨 If you’ve ever compared two Strings in Java and got unexpected results, you’re not alone 😅 Let’s break it down 👇 🔹 == 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿 • Compares references (memory addresses) • Checks if two variables point to the same object • Works fine for primitives, but tricky for objects 🔸 .𝗲𝗾𝘂𝗮𝗹𝘀() 𝗺𝗲𝘁𝗵𝗼𝗱 • Compares values (content) • Checks if two objects are logically equal • Can be overridden in classes for custom equality 💡 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: 𝘚𝘵𝘳𝘪𝘯𝘨 𝘢 = 𝘯𝘦𝘸 𝘚𝘵𝘳𝘪𝘯𝘨("𝘑𝘢𝘷𝘢"); 𝘚𝘵𝘳𝘪𝘯𝘨 𝘣 = 𝘯𝘦𝘸 𝘚𝘵𝘳𝘪𝘯𝘨("𝘑𝘢𝘷𝘢"); 𝘚𝘺𝘴𝘵𝘦𝘮.𝘰𝘶𝘵.𝘱𝘳𝘪𝘯𝘵𝘭𝘯(𝘢 == 𝘣); // 𝘧𝘢𝘭𝘴𝘦 (𝘥𝘪𝘧𝘧𝘦𝘳𝘦𝘯𝘵 𝘰𝘣𝘫𝘦𝘤𝘵𝘴) 𝘚𝘺𝘴𝘵𝘦𝘮.𝘰𝘶𝘵.𝘱𝘳𝘪𝘯𝘵𝘭𝘯(𝘢.𝘦𝘲𝘶𝘢𝘭𝘴(𝘣)); // 𝘵𝘳𝘶𝘦 (𝘴𝘢𝘮𝘦 𝘤𝘰𝘯𝘵𝘦𝘯𝘵) 📌 TL;DR: Use == for reference comparison, and .equals() for value comparison. #Java #CodingTips #Programming #SoftwareEngineering #Learning #javadevelopers
To view or add a comment, sign in
-
-
Day 2 of improving my Java basics. Tried something different today — finding the length of a string without using length() method. Used exception handling and charAt() to figure it out. Small problem, but it really made me think about how things work internally. =============================================== // Online Java Compiler // Use this editor to write, compile and run your Java code online class Main { public static void main(String[] args) { String s="Madhukar Pandey"; int count =0; boolean b=true; while(b) { try { s.charAt(count); count++; } catch(StringIndexOutOfBoundsException e ) { System.out.println("Length of string is:"+count); b=false; } } } }
To view or add a comment, sign in
-
-
🚨throw vs throws in Java - One Letter, Completely Different Meaning When I first started learning Java, two keywords confused me a lot: throw & throws They look almost identical... but they do very different things. Let's break it down simply👇 💠throw - Used to actually throw an exception -> Used within methods to explicitly raise an exception instance, allowing one checked or unchecked exception at a time. 🧩Example: if(age < 18){ throw new IllegalArgumentException("Age must be 18 or above"); } Here, the program immediately throws an exception. 💠throws - Used to declare possible exceptions -> Used in method signatures to declare one or more potential checked exceptions, signaling to the caller that the exception must be handled. 🧩Example: public void readFile() throws IOException { FileReader file = new FileReader("data.txt"); } This method itself does not handle the exception - it passes responsibilty to the caller. 🧠Simple way to remember throw->used inside a method (creates) throws->used in method declaration (warns) 💡Understanding this difference helps to: ✅Write cleaner APIs ✅Handle errors properly ✅Make the code easier for others to use. 💬 Quick question for Java developers here: Which exception confused you the most when you were starting out? NullPointerException still haunts many developers 😅 #Java #ExceptionHandling #JavaDeveloper #BackendDevelopment #Programming #SoftwareEngineering #LearningInPublic
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development