Your SlideShare is downloading. ×
Packages
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Packages

447
views

Published on

Published in: Technology, Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
447
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
46
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Avoid name conflicts Access control
  • Declare the package at the beginning of a file using the formpackagepackagename;2. Define the class that is to be put in the package and declare it public.Create a subdirectory under the directory where the main source files are stored.Store the listing as classname.java in the subdirectory created.Compile the file. This creates .class file in the subdirectoryJava supports a keyword called “package” for creatinguser-defined packages. The package statement mustbe the first statement in a Java source file (exceptcomments and white spaces) followed by one or moreclasses.Package name is “myPackage” and classes areconsidred as part of this package; The code is saved ina file called “ClassA.java” and located in a directorycalled “myPackage”.
  • Here, access is either public or not used.When no access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared.name is the name of the interface, and can be any valid identifier.The methods which are declared have no bodies.Each class that includes an interface must implement all of the methodsVariables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. They must also be initialized with a constant value.
  • Here, access is either public or not usedIf a class implements more than one interface, the interfaces are separated with a comma.The methods that implement an interface must be declared public.
  • It cannot extend classes
  • Transcript

    • 1. Package - is a collection of related classes and interfaces that provides access protection and namespace management.
    • 2.  Declare the package at the beginning of a file using the form: package packagename;  Example: package firstPackage; Public class FirstClass { //Body of the class }
    • 3. Two types of packages: 1. Java API packages 2. User defined packages
    • 4.  The general form of importing package is: import packagename.classname; or import Packagename.*;  Example: import myPackage.ClassA; import myPackage.secondPackage .ClassName; import myPackage.*;
    • 5.  Public : available/visible everywhere  Private : only visible within that class  Protected : visible every where except the nonsubclasses in other package
    • 6. package pack; class c2 { void show_c2() { System.out.println("show_c2"); } } public class c1 extends c2 { public void show_c1() { System.out.println("show_c1"); show_c2(); } }
    • 7. Cont….. import pack.c1; class dmn_pack { public static void main(String ar[]) { c1 c=new c1(); c.show_c1(); } }
    • 8. Interface    It defines a standard and public way of specifying the behavior of classes. It supports the concept of multiple inheritance. All methods of an interface are abstract methods.
    • 9.  To define an interface, we write: interface Interface Name [extends other interfaces] { constant declarations ; abstract method declarations ; } 
    • 10. Example: interface Animal { int a =10; public void eat( ); public void travel( ); }
    • 11.   Once an interface has been defined, one or more classes can implement that interface. This can be done as: Class classname implements interfacename { body of classname }
    • 12. public class Mammal implements Animal { public void eat() { System.out.println ("Mammal eats"); } public void travel() { System.out.println ("Mammal travels"); } public static void main(String args[ ]) { Mammal m = new Mammal( ); m.eat(); m.travel(); } }
    • 13.   An interface can extend another interface, similarly to the way that a class can extend another class. The extends keyword is used to extend an interface. Syntax: interface name2 extends name1 { body of name2 }