Docsity

Java Packages: Understanding the Role and Importance in Java Programming, Slides of Java Programming

Partial preview of the text

Download Java Packages: Understanding the Role and Importance in Java Programming and more Slides Java Programming in PDF only on Docsity!

ava - programming

An Introduction

Java Short Course Example

J

Example-Packages

  • A group of related classes
  • Each class in java belong to a specific package
  • Implemented as a folder on the disk
  • Purpose

 Unique class names  Application management  Complexity handling

  • Creating a Package

 package (keyword) package name  Should be the first statement in source file

  • Package Name Conventions

 Initial part: Internet domain name in reverse order (sun specification)  Last part: Any name that reflect the classes in package

  • Example

 Com.sun.2Dgraphics

  • Package name reflects the directory structure

package cont…

  • Fully qualified name

 Package name becomes part of class identity  A class name when written with its package name is termed as its fully qualified name  Example  my2Dgraphics.Circle  bscis.semester0.xyz.Circle  Directory structure reflected by two class files will be  outputpath/my2Dgraphics/Circle.class  Outputpath/bscis/semester0/xyz/Circls.class

  • A package can be imported to any other program using import

statement

 Import statement should placed before start of a class  Example  import javax.swings.JOptionPane;  Import javax.swings.*;

Classes and Objects Revisited

  • Class

 Declaration  Body

  • Class Declaration

 Access Specifier : public private protected  Class name  Super class name (parent class )  Interface (yet to discuss)  Keywords : class extends implements  Examples:  class Box [ just class name and class keyword]  public class Box [access specifier added ]  Public class Box extends Rectangle [ example of inheritance]  Public class Box implements printable [ interface usage]

controlling access….

  • Protected Members

 The keyword protected is written to make a member protected  A protected member is accessible to other classes with in the package  Outside the package only child classes can access a protected member  Not accessible to non-inherited classes outside the package

  • Default or Package access

 When no access specifieris used before a class member the access of that member function or variable is default or package  Such members are accessible to other classes within the package  Such members are not accessible to other classes outside the package