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
Java Upcasting and Downcasting Explained
More Relevant Posts
-
🚀 Building a Minimal Maven Java Build Tool (in Python) 👀 Not the whole thing — just the core ideas, in Python. Have a look at my GitHub Repo: https://lnkd.in/gtqey4Xp Started simple 👇 → A CLI tool to compile Java project using `javac` command → Packaging java code using `jar` command Then things got interesting… → Parsing `pom.xml` manually → Figuring out how dependencies are defined And now 👇 → Resolving dependencies using `groupId`, `artifactId`, `version` Next challenge: 👉 **Chained Dependency Resolution** (Dependencies of dependencies… recursively 😅) This made me realize — what looks like a simple `mvn clean install` is actually a chain of parsing, inheritance, and resolution working together. #Maven #Java #Python #BuildTools #LearningInPublic #BuildInPublic #SoftwareEngineer
To view or add a comment, sign in
-
Day 10/100 – Java Practice Challenge 🚀 Continuing my #100DaysOfCode journey with another important Java concept. 🔹 Topic Covered: Polymorphism Polymorphism means “many forms” — the same method behaves differently depending on the object. 💻 Practice Code: 🔸 Example Program class Animal { void sound() { System.out.println("Animal makes sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Animal a = new Dog(); // Upcasting a.sound(); // Runtime polymorphism } } 📌 Key Learnings: ✔️ Same method → different behavior ✔️ Achieved using method overriding ✔️ Based on object type (runtime) 🎯 Focus: Understanding dynamic behavior using inheritance and method overriding 🔥 Interview Insight: Polymorphism is a core OOP concept and widely used in real-world applications. #Java #100DaysOfCode #Polymorphism #OOP #JavaDeveloper #Programming #LearningInPublic
To view or add a comment, sign in
-
After years of Java, I finally tried Python. Honestly? I didn't expect to enjoy it this much. No semicolons. No curly braces. No type declarations. Just... clean, readable code that almost reads like English. As a Java developer, some things caught me off guard: → Returning multiple values without creating a class → List comprehensions replacing 5 lines with 1 → Decorators that actually execute code (unlike Java annotations) → Context managers that feel conversational I wrote about my first impressions — the good, the surprising, and where I still trust Java more. If you're a Java developer curious about Python, this one's for you. #Python #Java #SoftwareDevelopment #Programming #LearningInPublic
To view or add a comment, sign in
-
--->> Understanding Inheritance in Java & Its Types **Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows one class to acquire the properties and behaviors of another class. √ What is Inheritance? It is the process where a child class inherits variables and methods from a parent class using the extends keyword. ~Why is it Important? ✔️ Code reusability ✔️ Reduced development time ✔️ Better maintainability ✔️ Cleaner and scalable design @ Types of Inheritance in Java 1️⃣ Single Inheritance 2️⃣ Multilevel Inheritance 3️⃣ Hierarchical Inheritance 4️⃣ Hybrid (combination of types) # Important Notes 🔸 Java does NOT support multiple inheritance using classes ➡️ Because of the Diamond Problem (ambiguity in method resolution) 🔸 Cyclic inheritance is not allowed ➡️ Prevents infinite loops in class relationships 💻 Code Example (Single Inheritance) Java class Parent { void show() { System.out.println("This is Parent class"); } } class Child extends Parent { void display() { System.out.println("This is Child class"); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.show(); // inherited method obj.display(); // child method } } 👉 Here, the Child class inherits the show() method from the Parent class. -->> Real-World Example Think of a Vehicle system 🚗 Parent: Vehicle Child: Car, Bike All vehicles share common features like speed and fuel, but each has its own unique behavior. @ Key Takeaway Inheritance helps you avoid code duplication and build efficient, reusable, and scalable application TAP Academy #Java #OOP #Inheritance #Programming #JavaDeveloper #Coding #SoftwareDevelopment #LearnJava
To view or add a comment, sign in
-
-
Day 11/100 – Java Practice Challenge 🚀 Continuing my #100DaysOfCode journey with another important Java concept. 🔹 Topic Covered: Compile-time vs Runtime Polymorphism 💻 Practice Code: 🔸 Compile-time Polymorphism (Method Overloading) class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } } 🔸 Runtime Polymorphism (Method Overriding) class Animal { void sound() { System.out.println("Animal sound"); } } class Cat extends Animal { @Override void sound() { System.out.println("Cat meows"); } } public class Main { public static void main(String[] args) { // Compile-time Calculator c = new Calculator(); System.out.println(c.add(10, 20)); System.out.println(c.add(10, 20, 30)); // Runtime Animal a = new Cat(); a.sound(); } } 📌 Key Learnings: ✔️ Compile-time → method decided at compile time ✔️ Runtime → method decided at runtime ✔️ Overloading vs Overriding difference 🎯 Focus: Understanding how Java resolves method calls 🔥 Interview Insight: Difference between compile-time and runtime polymorphism is one of the most frequently asked Java interview questions. #Java #100DaysOfCode #MethodOverloading #MethodOverriding #Polymorphism #JavaDeveloper #Programming #LearningInPublic
To view or add a comment, sign in
-
📘 Day 9 of Java Learning Series 🔹 String vs StringBuilder vs StringBuffer If you're working with text in Java, understanding these 3 is very important 👇 🔸 1. String ✔ Immutable (cannot be changed) ✔ Every modification creates a new object ✔ Slower when modifying frequently 💡 Example: String s = "Hello"; s = s + " World"; 🔸 2. StringBuilder ✔ Mutable (can be changed) ✔ Faster than String ✔ Not thread-safe 💡 Example: StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); 🔸 3. StringBuffer ✔ Mutable ✔ Thread-safe (synchronized) ✔ Slightly slower than StringBuilder 💡 Example: StringBuffer sbf = new StringBuffer("Hello"); sbf.append(" World"); 🔸 Key Differences: ✔ String → Immutable ✔ StringBuilder → Fast & Non-Synchronized ✔ StringBuffer → Thread-Safe 💡 When to Use? ✔ Use String → when data doesn’t change ✔ Use StringBuilder → for performance (most cases) ✔ Use StringBuffer → in multi-threaded apps 💬 Which one do you use the most? 👉 Follow me for more Java content 🚀 #Java #Programming #100DaysOfCode #Developers #Learning #CoreJava
To view or add a comment, sign in
-
-
🚀 Exploring Method Overloading in Java As part of my journey in mastering Object-Oriented Programming in Java, I recently explored one of the most powerful concepts of Polymorphism — Method Overloading. 💡 What is Method Overloading? Method overloading is the process of creating multiple methods with the same name in a class, but with different parameter lists. It allows the same action to behave differently based on the input — making programs more flexible and readable. 🔹 Three Ways to Achieve Method Overloading A method can be overloaded by changing: 1️⃣ Number of parameters 2️⃣ Data types of parameters 3️⃣ Order/sequence of parameters ❌ Invalid Case If two methods have the same name + same parameters but different return types, it is NOT valid overloading and results in a compile-time error. Example: int area(int, int) float area(int, int) → Compilation Error 🚫 🧠 Why is it called False (Virtual) Polymorphism? To the user, it looks like one method performing multiple tasks (one-to-many). But internally, each call maps to a separate method (one-to-one) — hence the term False Polymorphism. ⚡ Type Promotion in Overloading If an exact match is not found, Java automatically promotes smaller data types to larger ones: byte → short → int → long → float → double This makes method overloading even more powerful and flexible! 👩💻 Simple Example class AreaCalculator { int area(int l, int b) { return l * b; } double area(double r) { return 3.14 * r * r; } int area(int side) { return side * side; } } TAP Academy ✨ Learning these core OOP concepts is helping me build stronger foundations in Java and improve my problem-solving skills step by step. #Java #OOP #Programming #CodingJourney #ComputerScience #LearningInPublic
To view or add a comment, sign in
-
-
🚫 Why Java Disallows Multiple Inheritance – The Diamond Problem Explained! Ever wondered why Java doesn’t support multiple inheritance with classes? 🤔 The answer lies in something called the Diamond Problem. 🔷 Imagine this: A class (Child) inherits from two parent classes (Parent A & Parent B), and both of them inherit from a common class (Object). Now, what happens if both parents have the same method? 👉 The child class gets duplicate methods 👉 The compiler gets confused 👉 And you get a compilation error ❌ 💥 This leads to ambiguity: Which method should the child use? Parent A’s or Parent B’s? 🔍 Key Insights: ✔ Every Java class already extends the Object class ✔ Multiple inheritance can lead to duplicate method injection ✔ Identical method signatures create conflicts the compiler can’t resolve ✔ Java follows a “zero tolerance for ambiguity” approach 💡 How Java Solves This? Instead of multiple inheritance with classes, Java uses: 👉 Interfaces (with default methods) 👉 Clear method overriding rules This ensures: ✅ Better code clarity ✅ No ambiguity ✅ Easier maintainability 🔥 Takeaway: Java prioritizes simplicity and reliability over complexity — and avoiding the Diamond Problem is a perfect example of that design philosophy. #TAPAcademy #Java #OOP #Programming #SoftwareDevelopment #Coding #JavaDeveloper #TechConcepts #LearningJourney
To view or add a comment, sign in
-
-
🚀 Exploring Method Overloading with Varargs in Java Today, I worked on an interesting Java problem that helped me strengthen my understanding of Compile-Time Polymorphism and Varargs (Variable Arguments). 🔹 Problem Statement: Design a VarargsCalculator class with overloaded methods to calculate the sum of: Multiple integers Multiple double values 🔹 Key Concepts Used: ✔ Method Overloading ✔ Varargs (int..., double...) ✔ Switch-case for dynamic method selection ✔ For-each loop for efficient iteration 🔹 Learning Outcome: This problem clearly demonstrates how Java allows the same method name to handle different types and number of inputs, depending on the parameters passed. It also shows how varargs simplify handling multiple inputs without explicitly creating arrays. 💡 Key Insight: Varargs in Java internally behave like arrays, making code more flexible and readable. 🔹 Sample Output: Sum of integers: 10 Sum of doubles: 7.0 📌 Practicing such problems is helping me improve my problem-solving skills and deepen my understanding of core Java concepts. #Java #CoreJava #MethodOverloading #Varargs #Programming #Coding #100DaysOfCode #DeveloperJourney #JavaDeveloper #LearningInPublic
To view or add a comment, sign in
-
-
🔹 Title: Solving “Plus Minus” Problem in Java 📊 🔹 Description: Today I solved the Plus Minus problem, where the goal is to calculate the ratios of positive, negative, and zero values in an array. The challenge was not just counting the values, but also formatting the output correctly to 6 decimal places. 💡 Approach: Traverse the array and count positives, negatives, and zeros Divide each count by the total number of elements Print results using precise formatting 🔹 What I learned: ✔ Importance of output formatting ✔ Handling edge cases (like zeros) ✔ Writing clean and efficient Java code Consistency in practicing such problems really strengthens core programming skills. 🚀 #Java #Coding #ProblemSolving #Programming #DataStructures
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