Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

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 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 “” 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 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
  • Packages

    1. 1. Package - is a collection of related classes and interfaces that provides access protection and namespace management.
    2. 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. 3. Two types of packages: 1. Java API packages 2. User defined packages
    4. 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. 5.  Public : available/visible everywhere  Private : only visible within that class  Protected : visible every where except the nonsubclasses in other package
    6. 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. 7. Cont….. import pack.c1; class dmn_pack { public static void main(String ar[]) { c1 c=new c1(); c.show_c1(); } }
    8. 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. 9.  To define an interface, we write: interface Interface Name [extends other interfaces] { constant declarations ; abstract method declarations ; } 
    10. 10. Example: interface Animal { int a =10; public void eat( ); public void travel( ); }
    11. 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. 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( );;; } }
    13. 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 }