Skip to main content

Package in Java| what is the case used for a package in java| creating packages in java

 Package in Java

Package is a group of similar types of classes, interfaces and sub-packages. Packages can be built-in or user defined.

Built-in packages - java, util, io etc.


import java.util.Scanner;


import java.io.IOException;


Access Modifiers in Java

  • Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.

  • Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.

  • Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.

  • Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.


package newpackage;

 

class Account {

   public String name;

   protected String email;

   private String password;

 

   public void setPassword(String password) {

       this.password = password;

   }

}

public class Sample {

   public static void main(String args[]) {

       Account a1 = new Account();

       a1.name = "Apna College";

       a1.setPassword("abcd");

       a1.email = "hello@apnacollege.com";

   }

}

 



Encapsulation 

Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, the data is not accessed directly; it is accessed through the functions present inside the class. In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible.(Data hiding: a language feature to restrict access to members of an object, reducing the negative effect due to dependencies. e.g. "protected", "private" feature in Java). 








Abstraction 

We try to obtain an abstract view, model or structure of a real life problem, and reduce its unnecessary details. With definition of properties of problems, including the data which are affected and the operations which are identified, the model abstracted from problems can be a standard solution to this type of problems. It is an efficient way since there are nebulous real-life problems that have similar properties. 

In simple terms, it is hiding the unnecessary details & showing only the essential parts/functionalities to the user.

Data binding : Data binding is a process of binding the application UI and business logic. Any change made in the business logic will reflect directly to the application UI. 

Abstraction is achieved in 2 ways :

  • Abstract class

  • Interfaces (Pure Abstraction)


  1. Abstract Class

  • An abstract class must be declared with an abstract keyword.

  • It can have abstract and non-abstract methods.

  • It cannot be instantiated.

  • It can have constructors and static methods also.

  • It can have final methods which will force the subclass not to change the body of the method.

abstract class Animal {

   abstract void walk();

   void breathe() {

       System.out.println("This animal breathes air");

   }

   Animal() {

       System.out.println("You are about to create an Animal.");

   }

}

 

class Horse extends Animal {

   Horse() {

       System.out.println("Wow, you have created a Horse!");

   }

   void walk() {

       System.out.println("Horse walks on 4 legs");

   }

}

 

class Chicken extends Animal {

   Chicken() {

       System.out.println("Wow, you have created a Chicken!");

   }

   void walk() {

       System.out.println("Chicken walks on 2 legs");

   }

}

 

public class OOPS {

   public static void main(String args[]) {

      Horse horse = new Horse();

      horse.walk();

      horse.breathe();

   }

}







2. Interfaces

  • All the fields in interfaces are public, static and final by default.

  • All methods are public & abstract by default.

  • A class that implements an interface must implement all the methods declared in the interface.

  • Interfaces support the functionality of multiple inheritance.

interface Animal {

   void walk();

}

 

class Horse implements Animal {

   public void walk() {

       System.out.println("Horse walks on 4 legs");

   }

}

 

class Chicken implements Animal {

   public void walk() {

       System.out.println("Chicken walks on 2 legs");

   }

}

 

public class OOPS {

   public static void main(String args[]) {

      Horse horse = new Horse();

      horse.walk();

   }

}

 


Static Keyword 

Static can be : 

  1. Variable (also known as a class variable)

  2. Method (also known as a class method)

  3. Block

  4. Nested class

class Student {

   static String school;

   String name;   

}

 

public class OOPS {

   public static void main(String args[]) {

       Student.school = "JMV";

       Student s1 = new Student();

       Student s2 = new Student();

 

       s1.name = "Meena";

       s2.name = "Beena";

 

       System.out.println(s1.school);

       System.out.println(s2.school);

   }

}



Comments

Popular posts from this blog

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

2D Arrays | Java Complete Placement Course | Lecture 11

Java - Introduction to Programming Lecture 11 2D Arrays In Java It is similar to 2D matrices that we studied in 11th and 12th class. Creating a 2D Array - with new keyword int [][] marks = new int [ 3 ][ 3 ] ; Taking a matrix as an input and printing its elements. import java . util .*;   public class TwoDArrays {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int rows = sc . nextInt ();         int cols = sc . nextInt ();           int [][] numbers = new int [ rows ][ cols ];           //input         //rows         for ( int i = 0 ; i < rows ; i ++) {             //columns  ...

Merge Sort | How do I learn merge sort?

       Merge Sort | For Beginners | Java Placement Course >> Merge sort is a popular sorting algorithm that utilizes a divide-and-conquer approach to sort elements in an array. The basic idea behind merge sort is to divide an unsorted array into two halves, recursively sort each half, and then merge the two sorted halves into a single sorted array. >>  Merge sort algorithm works by recursively dividing an array into two halves, sorting each half, and then merging them to obtain the sorted array. The basic steps of the merge sort algorithm are as follows: Divide the unsorted array into two halves, mid and left. Sort the left half recursively using merge sort algorithm. Sort the right half recursively using merge sort algorithm. Merge the two sorted halves to obtain the final sorted array. JAVA Implementation: // Merge sort public class MergeSort {     //time complexity=nlogn     public static void conquer ( int arr [], int ...