You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+68Lines changed: 68 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -284,6 +284,8 @@ In java,it is possible to define two or more constructor of the same class that
284
284
285
285
**Code4:[ConstructorOverloading.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/Constructors/ConstructorOverloading.java):** Java program to demostrate constructor overloading.
286
286
287
+
See more on contructors: [Rules And Properties of Constructor](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/Classes%20and%20objects/RulesAndPropertiesOfConstructor.txt)
288
+
287
289
# ENCAPSULATION:
288
290
289
291
Encapsulation is the mechanism that binds together code and the data it manipulates.Other way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield.
@@ -304,6 +306,72 @@ Encapsulation is the mechanism that binds together code and the data it manipula
304
306
305
307
4.**Testing code:** Encapsulated code is easy to test for unit testing.
306
308
309
+
# PACKAGES:
310
+
311
+
A package is a container within which we can store multiple classes,subpackages and interfaces.A package is a container of a group of related classes where some of the classes are accessible are exposed and others are kept for internal purpose.
312
+
313
+
Packages are used for:
314
+
315
+
1.Avoiding namespace collision ie Preventing naming conflicts.Thus there can be two classes of same name in two different packages.
316
+
2.Searching and using classes, interfaces, enumerations and annotations becomes easier
317
+
3.Providing controlled access: protected and default have package level access control. A protected member is accessible by classes in the same package and its subclasses. A default member (without any access specifier) is accessible by classes in the same package only.
318
+
4.Packages can be considered as data encapsulation (or data-hiding).
319
+
320
+
**How packages work?**
321
+
322
+
If a package name is college.dept.cse, then there are three directories, college, dept and cse such that cse is present in dept and dept is present college. Also, the directory college is accessible through **CLASSPATH** variable, i.e., path of parent directory of college is present in CLASSPATH. The idea is to make sure that classes are easy to locate.We can add more classes to a created package by using package name at the top of the program and saving it in the package directory. We need a new java file to define a public class, otherwise we can add the new class to an existing .java file and recompile it.
323
+
324
+
// import the Vector class from util package.
325
+
import java.util.vector;
326
+
327
+
// import all the classes from util package
328
+
import java.util.*;
329
+
// All the classes and interfaces of this package
330
+
// will be accessible but not subpackages.
331
+
import package.*;
332
+
333
+
// Only mentioned class of this package will be accessible.
334
+
import package.classname;
335
+
336
+
// Class name is generally used when two packages have the same
337
+
// class name. For example in below code both packages have
338
+
// date class so using a fully qualified name to avoid conflict
339
+
import java.util.Date;
340
+
import my.packag.Date;
341
+
342
+
Packages can be of two types:
343
+
344
+
**Built-in Packages:**
345
+
346
+
These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built-in packages are:
347
+
1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This package is automatically imported.
348
+
349
+
2) java.io: Contains classed for supporting input / output operations.
350
+
351
+
3) java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations.
352
+
353
+
4) java.applet: Contains classes for creating Applets.
354
+
355
+
5) java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc).
356
+
357
+
6) java.net: Contain classes for supporting networking operations.
358
+
359
+
**Points to be noted:**
360
+
361
+
1.Every class is part of some package.
362
+
2.If no package is specified, the classes in the file goes into a special unnamed package (the same unnamed package for all files).
363
+
3.All classes/interfaces in a file are part of the same package. Multiple files can specify the same package name.
364
+
4.If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name).
365
+
5.We can access public classes in another (named) package using: package-name.class-name
366
+
367
+
**User-defined packages:**
368
+
369
+
These are the packages that are defined by the user. First we create a directory with the desired package name (name should be same as the name of the package). Then we create the desired class inside the directory with the first statement being the package names.
370
+
371
+
Static import is a feature introduced in Java programming language ( versions 5 and above ) that allows members ( fields and methods ) defined in a class as public static to be used in Java code without specifying the class in which the field is defined.
372
+
373
+
**Code3::[StatisImport.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/StaticImport.java):** A program to illustrate how Static import works.
0 commit comments