Coming from Java, I was always thinking like this “One class can extend only one class”. If I need more behavior, I need to use interfaces. Then, when I started Dart, I got confused again 😅 'extends' felt familiar, just like Java: If Dog extends Animal → Dog automatically gets eat(), sleep() from Animal So no confusion here. It’s simple inheritance. But then I saw “with … mixins” the inheritance things changed for me. Mixin in Dart = reuse behavior hastily Example: “class Dog extends Animal with Run, Swim” Now Dog can run AND swim without needing inheritance 'Run' and 'Swim' Here, Dog != Run, also Dog !=Swim still 'Dog' can access methods of Run and Swim. This is the power of 'Mixin' Also in Java, multiple inheritance is not allowed, to do that I would use interfaces and default methods for this. But in Dart I can indirectly use methods from multiple classes through mixins. Also in mixins, I can define that which class can access my mixin class by 'on' keyword. This is one of the interesting power mixin has, where normal interface implementation do not have such type of restriction power. That’s the difference, the simple way I understand now: extends = what the object is. mixin = what the object can do. #Flutter #Dart #Java #Mixin #OOP #LearningInPublic
Dart Mixins vs Java Inheritance
More Relevant Posts
-
Day 8/100 – Java Practice Challenge 🚀 Continuing my #100DaysOfCode journey with another important Java concept. 🔹 Topic Covered: Upcasting and Downcasting Upcasting and downcasting are important concepts in Java that deal with type conversion between parent and child classes. They are widely used in real-world applications, especially in polymorphism. 💻 Practice Code: 🔸 Example Program class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { // 🔹 Upcasting (Child → Parent) Animal a = new Dog(); a.sound(); // 🔹 Downcasting (Parent → Child) Dog d = (Dog) a; d.bark(); } } 📌 Key Learnings: ✔️ Upcasting is automatic and safe ✔️ Downcasting requires explicit casting ✔️ Downcasting can cause ClassCastException if not handled properly ✔️ Helps achieve runtime polymorphism 🎯 Focus: Understanding how objects behave when referenced by parent vs child types ⚡ Types of Casting: 👉 Upcasting (Implicit) 👉 Downcasting (Explicit) 🔥 Interview Insight: Upcasting and downcasting are frequently asked in interviews to test your understanding of polymorphism and object behavior in Java. #Java #100DaysOfCode #Upcasting #Downcasting #JavaDeveloper #Programming #LearningInPublic
To view or add a comment, sign in
-
Your switch-case multiplied again. Here's the refactoring that kills it. When your type code only affects data - not behavior - you can replace switch-case with a class. Not if-else chains, not pattern matching. A class where each type is an instance that carries its own data. This works in Java, JavaScript, Kotlin, C# - any language with classes. The idea is simple: if price, weight, and label all depend on a type code, why scatter that knowledge across three switch statements? Put it in one place. Let the constructor enforce completeness. Add a new type? Create a new instance. The compiler tells you what's missing. No grep. No "did I forget a case somewhere". (When types also change behavior, you need a different approach. That's the next post, next week.) I walked through the full refactoring with a pizza example. From int constants to enums to smart classes. Link in the first comment. #cleancode #refactoring #softwarecraft #codequality
To view or add a comment, sign in
-
-
𝐯𝐚𝐫 𝐤𝐞𝐲𝐰𝐨𝐫𝐝 𝐢𝐧 𝐉𝐚𝐯𝐚 Introduced in Java 10, the var reserved type name is a game-changer for reducing boilerplate code—but it comes with specific "rules of the road." Is it a keyword? Technically, no! It’s a 𝐫𝐞𝐬𝐞𝐫𝐯𝐞𝐝 𝐭𝐲𝐩𝐞 𝐧𝐚𝐦𝐞 that uses 𝐓𝐲𝐩𝐞 𝐈𝐧𝐟𝐞𝐫𝐞𝐧𝐜𝐞 to automatically detect data types based on the context. Here is a quick cheat sheet on the Dos and Don'ts: 𝐖𝐡𝐞𝐧 𝐭𝐨 𝐮𝐬𝐞 '𝐯𝐚𝐫': 𝐋𝐨𝐜𝐚𝐥 𝐕𝐚𝐫𝐢𝐚𝐛𝐥𝐞𝐬: Use it inside methods, blocks, or constructors. 𝐒𝐭𝐚𝐧𝐝𝐚𝐫𝐝 𝐃𝐚𝐭𝐚 𝐓𝐲𝐩𝐞𝐬: Works for int, double, String, etc., as long as an initializer is present. 𝐂𝐥𝐞𝐚𝐧𝐢𝐧𝐠 𝐮𝐩 𝐆𝐞𝐧𝐞𝐫𝐢𝐜𝐬: Turn long declarations into clean, readable lines. 𝐖𝐡𝐞𝐫𝐞 '𝐯𝐚𝐫' 𝐢𝐬 𝐍𝐎𝐓 𝐚𝐥𝐥𝐨𝐰𝐞𝐝: 𝐈𝐧𝐬𝐭𝐚𝐧𝐜𝐞 𝐕𝐚𝐫𝐢𝐚𝐛𝐥𝐞𝐬: You cannot use it for class-level fields. 𝐌𝐢𝐬𝐬𝐢𝐧𝐠 𝐈𝐧𝐢𝐭𝐢𝐚𝐥𝐢𝐳𝐞𝐫𝐬: You can't just declare var x;. The compiler needs to see the value immediately. 𝐍𝐮𝐥𝐥 𝐕𝐚𝐥𝐮𝐞𝐬: var x = null; won't work because the compiler can't infer a type from null. 𝐋𝐚𝐦𝐛𝐝𝐚𝐬: These need an explicit target type, so var is a no-go here. 𝐌𝐞𝐭𝐡𝐨𝐝 𝐒𝐩𝐞𝐜𝐬: It cannot be used for method parameters or return types. 𝐓𝐡𝐞 𝐆𝐨𝐥𝐝𝐞𝐧 𝐑𝐮𝐥𝐞: var is meant to improve 𝐫𝐞𝐚𝐝𝐚𝐛𝐢𝐥𝐢𝐭𝐲. If using it makes the code harder to understand, stick to explicit types! Special thanks to Syed Zabi Ulla Sir for the clear breakdown and guidance on these core Java concepts! #Java #Programming #CodingTips #BackendDevelopment #Java10 #SoftwareEngineering #CleanCode
To view or add a comment, sign in
-
-
A common pattern in FastAPI code bases, one that is used in all example code, is to have a global variable which represents the SQL connection pool. The only problem with this pattern is making sure no class or function pulls it in directly, inline, where it can'tbe mocked. It's hard because so much of your code will depend on it. You'll quickly see why Java and C# used dependency injection as a standard design pattern. The question, then, is "Why?" And the answer is simply all your "unit" tests will now require a DB instance. Nothing says "fun" like watching all your test code blow up because someone forgot this (sometimes you're the someone.) Then you get to unravel layer upon layer of code. Claude can't do it all for you. #code #python
To view or add a comment, sign in
-
🐫 Camel Case vs 🐍 Snake Case | Master Naming Conventions for Cleaner Code Ever stared at code and wondered why some variables look like userId while others are user_name? 🤔 It all comes down to naming conventions! Our detailed guide breaks down camelCase and snake_case in a way that is easy to understand and apply: ✅ What camelCase is and why it’s used in JavaScript, Java, and modern APIs ✅ What snake_case is and why Python, databases, and backend systems love it ✅ Real-world examples you’ll actually encounter in projects ✅ Simple tips to decide which style fits your team or project If you want your code, text, or data naming to be consistent, readable, and professional, this is a must-read! Click below to see the full comparison and start applying it today 💻✨ 🔗 Read the full guide here: https://lnkd.in/d9vTEPfe
To view or add a comment, sign in
-
-
Day 73 of #90DaysDSAChallenge Solved LeetCode 451: Sort Characters By Frequency Learned an important Java design concept today. Problem Overview: The task was to sort characters in a string based on descending frequency. What confused me initially: Why create a separate Freq class instead of just using HashMap and PriorityQueue directly? Key Learning: PriorityQueue stores one complete object at a time. For this problem, each item needs two pieces of data together: Character Frequency Example: Instead of storing: e and 2 separately We package them as: Freq('e', 2) That custom class acts like a container holding both values in one object, so PriorityQueue can compare and sort them correctly. Why this matters: This taught me that custom classes in Java are often not about complexity, they simply bundle related data into one manageable unit. Alternative approach: We can also use Map.Entry<Character, Integer> instead of creating a custom class, but building Freq makes the logic easier to understand while learning. Today’s takeaway: Not every class is for business logic — sometimes it exists just to package data cleanly. #Java #90DaysDSAChallenge #LeetCode #PriorityQueue #HashMap #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
I was tired of having to open my bloated, local IDE to test my code during Codeforces contests, and repetitively typing lines of test cases into STDIN gets really annoying after some time. So, I built an online compiler that speeds up that process and automates the grunt work. It's build on top of a Containerization API that I made myself, and so it's immune to most attacks like fork bombs or disk-fillers, It also makes it easier to visualize the working of that API. The project supports Python, C++, Java and C, the 4 biggest languages on codeforces, feel free to use it on it's deployed URL: https://lnkd.in/g2DB2mPw github repo for the online compiler: https://lnkd.in/g2dkNy3H github repo for the api: https://lnkd.in/g9YPisst
To view or add a comment, sign in
-
-
🚀 DAY 76/150 — CHECKING SUBTREES WITH RECURSION! 🌳 Day 76 of my 150 Days DSA Challenge in Java and today I solved a problem that strengthens my understanding of tree comparison and recursion 💻🧠 📌 Problem Solved: Subtree of Another Tree 📌 LeetCode: #572 📌 Difficulty: Easy–Medium The task is to determine whether one binary tree is a subtree of another binary tree. A subtree must match both structure and node values exactly. 🔹 Approach Used I used a recursive DFS approach: • Traverse each node of the main tree • For every node: Check if the subtree starting at that node is identical to the given tree • Use a helper function to compare two trees: If both nodes are null → true If values differ → false Recursively compare left and right subtrees ⏱ Complexity Time Complexity: O(n × m) (n = nodes in main tree, m = nodes in subtree) Space Complexity: O(h) (recursion stack) 🧠 What I Learned • Tree problems often require combining traversal + comparison • Recursion is powerful for handling hierarchical structures like trees • Breaking the problem into smaller checks simplifies the solution 💡 Key Takeaway This problem taught me how to: Compare two trees efficiently Apply recursion for structural matching Think in terms of subproblems within trees 🌱 Learning Insight As I continue my Binary Tree journey, I’m understanding that: Most tree problems are based on DFS, BFS, or recursion patterns Mastering these basics makes complex tree problems easier ✅ Day 76 completed 🚀 74 days to go 🔗 Java Solution on GitHub: 👉 https://lnkd.in/gZNXaW-C 💡 In trees, solving smaller parts correctly leads to the full solution. #DSAChallenge #Java #LeetCode #BinaryTree #Recursion #DFS #150DaysOfCode #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Solved Find Peak Element -> LeetCode(162) Medium, Binary Search in Java. Till now I had solved around 10-11 binary search problems. But in all of them array was always sorted , even rotated ones had at least one sorted half. So I had one strong assumption , binary search only works on sorted arrays. This problem broke that assumption completely. No sorted half, no rotation logic working. I was stuck because none of my previous patterns were fitting here. Took help from Claude. It suggested slope based thinking , if right neighbour is greater, peak is on right side, go right. If left neighbour is greater, go left. If both neighbours are smaller, current element is peak. Then I questioned > what if slope breaks in between? Claude pointed me to re-read the problem. The key insight was that array has -∞ at both ends virtually, which guarantees at least one peak always exists. Applied my own logic from there and got it accepted Then Claude showed me a cleaner version , when low < high, at the point where low == high we already have our peak. No need for extra boundary checks. That gave me a second shorter solution. Also broke my second assumption > binary search doesn't always need while(low <= high). Sometimes while(low < high) is cleaner. Two wrong assumptions fixed in one problem 🙌 Took help but questioned it, understood it, then coded it myself. Github Repo link : https://lnkd.in/grF5ACw5 **Any other wrong assumption you had about binary search? Drop it below 👇** #DSA #Java #LeetCode #BinarySearch #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