Sunday, 8 January 2017

Abstarction :

Abstraction is process of hiding the implementation details and showing only the functionality.
·Abstraction in java is achieved by using interface and abstract class. Interface give 100% abstraction and abstract class give 0-100% abstraction.


Real Time Example :

  • Take the same example of news channel. The article they write on newspaper is abstracted as the heading. Hence the simple heading of the whole article is abstracted.
  • All are performing operations on the ATM machine like cash withdrawal, money transfer, retrieve mini-statement…etc. but we can't know internal details about ATM.

Friday, 11 November 2016

Association ,Composition and Aggregation

Association, Composition and Aggregation

1. Association :

    • Association is a relationship between two or more classes (objects) that shows how they are connected.
    • Unlike composition or aggregation, association does not imply ownership or dependency — it just represents a link.
    • Associations can be:
      • One-to-One
      • One-to-Many
      • Many-to-One
      • Many-to-Many

    ๐Ÿ“˜ Real-life Example :

    • Student is associated with a Faculty.
    • The Faculty teaches Students, and Students learn from the Faculty.
    • Both can exist independently, but they share a relationship.
    UML Diagram :

    Student  ↔  Faculty
    (Simple line = Association)
    Example :

    class Student {
        private String name;
    
        Student(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    }
    
    class Faculty {
        private String name;
    
        Faculty(String name) {
            this.name = name;
        }
    
        public void teach(Student student) {
            System.out.println(name + " teaches " + student.getName());
        }
    }
    
    public class AssociationExample {
        public static void main(String[] args) {
            Student s1 = new Student("Ravi");
            Faculty f1 = new Faculty("Dr. Sharma");
    
            // Association: Faculty teaches Student
            f1.teach(s1);
        }
    }
    Output :

    Dr. Sharma teaches Ravi
    ๐Ÿ”‘ Key Point
    • Association just connects objects — neither object’s lifecycle depends on the other.
    • If one is destroyed, the other can still exist.

    Composition vs Aggregation

    When designing classes in Object-Oriented Programming (OOP), we often deal with “has-a” relationships. These can be either Composition or Aggregation. Although both represent associations, the key difference lies in the dependency of objects.

    ✅ 2. Composition

    • Definition: A strong “has-a” relationship.
    • The contained object cannot exist without the container.
    • If the container is destroyed, the contained objects are also destroyed.

    ๐Ÿ“˜Real-life Example:

    • University consists of several Departments.
    • If the University object is destroyed, all its Department objects will also be destroyed.
    • Since a Department cannot exist without its University, this is a strong association, i.e., composition.
    UML Diagram :

    University ◆── Department
    (black diamond = Composition)
    Example :

    class Department {
        private String name;
    
        Department(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    }
    
    class University {
        private List<Department> departments;
    
        University(List<Department> departments) {
            this.departments = departments;
        }
    
        public void showDepartments() {
            for (Department d : departments) {
                System.out.println(d.getName());
            }
        }
    }
    
    public class CompositionExample {
        public static void main(String[] args) {
            Department d1 = new Department("Computer Science");
            Department d2 = new Department("Mechanical");
            List<Department> deptList = List.of(d1, d2);
    
            University university = new University(deptList);
            university.showDepartments();
            // When university is destroyed, departments go away too.
        }
    }

     3. Aggregation :

    • Definition: A weak “has-a” relationship.
    • The contained object can exist independently of the container.
    • Destroying the container does not destroy the contained objects.

      ๐Ÿ“˜ Real-life Example

      • Department may have several Professors.
      • If the Department is closed, the Professor objects can still exist independently (they may move to another department).
      • Since a Professor can exist without a specific Department, this is a weak association, i.e., aggregation.

      UML Diagram :

      Department ◇── Professor
      (white diamond = Aggregation)
      Example :

      class Professor {
          private String name;
      
          Professor(String name) {
              this.name = name;
          }
      
          public String getName() {
              return name;
          }
      }
      
      class DepartmentWithProfessor {
          private List<Professor> professors;//Aggregation
      
          DepartmentWithProfessors(String deptName, List<Professor> professors) {
              this.deptName = deptName;
              this.professors = professors;
          }
      
          public void showProfessors() {
              System.out.println("Department: " + deptName);
              for (Professor p : professors) {
                  System.out.println("Professor: " + p.getName());
              }
          }
      }
      
      public class AggregationExample {
          public static void main(String[] args) {
              Professor p1 = new Professor("Dr. Sharma");
              Professor p2 = new Professor("Dr. Singh");
      
              List<Professor> profList = List.of(p1, p2);
      
              DepartmentWithProfessors dept = new DepartmentWithProfessors("Computer Science", profList);
              dept.showProfessors();
              // Even if Department is removed, Professors still exist.
          }
      }


      ๐Ÿ”‘ Key Differences Between Composition and Aggregation:

      Feature

      Composition (Strong)

      Aggregation (Weak)

      Dependency

      Contained objects depend on container

      Contained objects independent

      Lifetime

      Destroyed when container is destroyed

      Can exist even if container is destroyed

      UML Notation

      Black Diamond ()

      White Diamond ()

      Example

      University – Departments

      Department – Professors


      ๐Ÿ“ Final Thoughts

      • Association is a general relationship between two objects. Both can exist independently, but they are linked (e.g., StudentFaculty).
      • Aggregation is a special type of association where objects have a weak dependency (e.g., DepartmentProfessor). The contained object can still exist independently.
      • Composition is a strong form of association where objects have a strong dependency (e.g., UniversityDepartment). If the container is destroyed, the contained objects are destroyed too.

      Monday, 31 October 2016

      Polymorphism

      Polymorphism

      Polymorphism means “many forms” (poly = many, morph = forms).
      It is the ability of an object or method to take different forms and perform a single action in multiple ways.

      In Java, polymorphism is categorized into two types:

      1. Compile-time Polymorphism (also called Static Polymorphism)

      2. Runtime Polymorphism (also called Dynamic Polymorphism)

      Polymorphism in Java can be achieved through:



      Tuesday, 6 September 2016

      Environment Setup

      Set the Path :

      1.Go on my computer icon and right click, after that click on properties option.

      2.Now click on Advance Settings



      3.Click on advance option



      4.Click on Environment Variable.



      5.Click on new button



      6.Now one dialog box is appear, now ignore this but don't close




      7.Now open my computer open c:/ > Programs Files > Java > jdk1.8.0_144 > bin copy this path



      C:\Program Files\Java\jdk1.8.0_144\bin

      Now come back on previous open dilogbox and write variable name 'path' and for variable value paste all copied path upto the bin folder. Put .; at the end. It (.) selects all the tools from the bin folder.




      Thursday, 11 August 2016

      What are different ways of iterating over keys, values and entry in Map?

      What are different ways of iterating over keys, values and entry in Map?

      Answer: 
      Create and put key-value pairs in HashMap

      package com.tutorial4u;

      import java.util.HashMap;
      import java.util.Iterator;
      import java.util.Map;

      public class HashMapDemo {
           public static void main(String[] args) {
                 Map hashMap = new HashMap();
                 hashMap.put(11, "CSS");
                 hashMap.put(21, "IT");
                 hashMap.put(31, "ECE");
                 hashMap.put(41, "EEE");
           }
          
      }

      Iterate over keys :-

      hashMap.keySet().iterator() method returns iterator to iterate over keys in HashMap.



      Iterator keyIterator =hashMap.keySet().iterator();
                 while(keyIterator.hasNext()){
                 System.out.println(keyIterator.next());
             }

      /*OUTPUT

      21
      41
      11

      31
      */
      Iterate over values :-


      hashMap.values().iterator() method returns iterator to iterate over keys in HashMap.


      Iterator valueIterator=hashMap.values().iterator();
                 while(valueIterator.hasNext()){
                 System.out.println(valueIterator.next());
             }

      /*OUTPUT

      IT
      EEE
      CSS

      ECE
      */

      Iterate over Entry-


      hashMap.entrySet().iterator() method returns iterator to iterate over keys in HashMap.

      Iterator entryIterator=hashMap.entrySet().iterator();
                 while(entryIterator.hasNext()){
                 System.out.println(entryIterator.next());
             }

      /*OUTPUT
      21=IT
      41=EEE
      11=CSS
      31=ECE

      */

      Java Development Kit (JDK) and Java Runtime Environment (JRE)

                        Java Development Kit (JDK) and Java Runtime Environment (JRE)  To download and install the Java Development Kit (  JDK ) ...