Powered by Blogger.
Showing posts with label learn java. Show all posts
Showing posts with label learn java. Show all posts

Java Code | Definition of Homogeneous Collection in Java Program

Definition of Homogeneous Collection:-

Homogeneous collection is the collection of objects that have a common class.

 Example of Homogeneous Collection:-

 public class TestHomogeneous{
   public static void main(String[]args){
      TestHomogeneous[] arr = new TestHomogeneous[3];
      arr[0] = new TestHomogeneous();
      arr[1] = new TestHomogeneous();
      arr[2] = new TestHomogeneous();
   }
 }


Homogeneous Collection:-

1. A collection of objects with the same dynamic type. Arrays are the most common homogeneous collection objects. See heterogeneous collection.

2. Browse Related Terms: array, autoboxing, base type, casting, dynamic type, heterogeneous collection, main method, return type.

Homogeneous Collection Programs:-

 class Man{
    public void fly(){
    System.out.print(DIP);
  }
 }
 public class A{
    public static void main(String[]args){
       Man[] arr = new Man[3];
       arr[0] = new Man();
       arr[1] = new Man();
       arr[2] = new Man();
   }
 }

OOPs in Java - Definition of Encapsulation Example with Program

Definition of Encapsulation:-

Encapsulation is the technique of hiding some members of a class from other classes but provides a public interface to access that members.

General Definition:-

In general, encapsulation is one of the 4 fundamentals of OOP (object-oriented programming). Encapsulation is to hide the variables or something inside a class, preventing unauthorized parties to use. So the public methods like getter and setter access it and the other classes call these methods for accessing.

This mechanism is not unique to object-oriented programming. Implementations of abstract data types, e.g. modules, offer a similar form of encapsulation. This similarity stems from the fact that both notions rely on the same mathematical fundament of an existential.

Example of Encapsulation Program:

 class MyNumber{
   private int number;

   public void setNumber(int number){
     this.number=number;
   }

   public int getNumber(){
     return number;
   }
   }

   public class EncapTest{
     public static void main(String[]args){
       MyNumber my = new MyNumber();
       my.setNumber(45);
       System.out.println(my.getNumber());
   }
  }

At line 2, attribute number is a private member of MyNumber class, so this attribute will not be accessible form other classes. But from the EncapTest class we are accessing the number variable of MyNumber class using set and get methods of MyNumber class.

Java Solution | Write a Java Program Using String Concatenation with +

String Concatenation with +: 

The + operator performs a concatenation of String objects, producing a new String.

 public class StringConTest{
 public static void main(String[]args){
 String salutation=”Mr.”;
 String name=”Dewan”+”Md.”+”Saju”;
 String title=salutation+name;
 System.out.println(title);
  }
 }