Java Programming basic part 3 - Packages in Java Language...

Info icon This preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Packages in Java Language Usman Saleem
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(c) Usman Saleem, 2002 2 Packages A package is a collection of related classes and interfaces providing access protection and namespace management.
Image of page 2
(c) Usman Saleem, 2002 3 Why Packages To make classes easier to find and to use To avoid naming conflicts To control access Programmers bundle groups of related classes and interfaces into packages The classes and interfaces that are part of the Java platform are members of various packages that bundle classes by function (examples…)
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(c) Usman Saleem, 2002 4 Creating a Package To place a class in a package, put the package statement at the top of the source file where the class is defined. e.g. we can define the Registrar class in Jinnah package package Jinnah; public class Registrar { } The package statement must be the first statement in the source file
Image of page 4
(c) Usman Saleem, 2002 5 Creating a Package (Cont.) The scope of the package statement is the entire source file. If you put multiple classes in a single source file, only one may be public, and it must share the name of the source files base name. Only public package members are accessible from outside the package. Classes within package may have unrestricted access to each other.
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(c) Usman Saleem, 2002 6 Creating a Package (Cont.) If you do not use a package statement, your class or interface ends up in the default package , which is a package that has no name. The default package is usually used only for small or temporary applications or when you are just beginning development.
Image of page 6
(c) Usman Saleem, 2002 7 Naming a Package Different programmers may write different classes with same name. Package may provide a solution. What if package also bears same name? Programmers usually follow a convention.
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(c) Usman Saleem, 2002 8 Naming a Package (Cont.) By Convention: Companies use their reversed Internet domain name in their package names, like this: com.company.package. Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name, for example, com.company.region.package.
Image of page 8
(c) Usman Saleem, 2002 9 Using Package Members
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern