Lecture 06 Java Classes and Packages

download Lecture 06 Java Classes and Packages

of 26

Transcript of Lecture 06 Java Classes and Packages

  • 8/11/2019 Lecture 06 Java Classes and Packages

    1/26

    Lecture 6

  • 8/11/2019 Lecture 06 Java Classes and Packages

    2/26

    To make types easier to find and use, to avoid naming conflicts, and to

    control access, programmers bundle groups of related types intopac ages.

    Java classes always exist in a class package There is a default package which doesnt have a name

    Java core API is made up of several packages Class names in a package are qualified by the package name e.g Math class has the fully qualified name as java.lang.Math

    Introduction to Java2

  • 8/11/2019 Lecture 06 Java Classes and Packages

    3/26

    The names of your classes and interfaces won't conflict with the

    names in other packages because the package creates a newnamespace.

    Names used for classes in one package will not interfere with the

    names of classes in another package

    Copackage classes enjoy special access to each other members ou on t ave to mport t ose c asses

    Introduction to Java3

  • 8/11/2019 Lecture 06 Java Classes and Packages

    4/26

    Creating packages Add a package statement as the first statement in your source file

    containing the class definition n y commen s an an nes are a owe o prece e e pac age

    statement

    A package statement consist of the keyword package followed byt e pac age name terminate y a semico on.

    You can specify a package name as a sequence of names separatedb eriods. If you do not use a package statement, your type ends up in an

    unnamed package Use an unnamed acka e onl for small or tem orar a lications

    Packages are intimately related to the directory structure in whichthey are stored

    Introduction to Java4

    Class files must be in a directory named by the package

  • 8/11/2019 Lecture 06 Java Classes and Packages

    5/26

    as the first line of the code (except comments)acka e < acka eName>

    package myownpackage;

  • 8/11/2019 Lecture 06 Java Classes and Packages

    6/26

    acka e SchoolClasses

    public class StudentRecord {private String name;

    private String address;private int age;.

    }

  • 8/11/2019 Lecture 06 Java Classes and Packages

    7/26

    To use a public package member (classes and

    interfaces) from outside its package, you mustdo one of the following Import the package member using import

    statement Import the member's entire package using import

    statement Refer to the member by its fully qualified name

    (without using import statement)

  • 8/11/2019 Lecture 06 Java Classes and Packages

    8/26

  • 8/11/2019 Lecture 06 Java Classes and Packages

    9/26

    interpreter expects the directory structure containingthe executable classes to match the package hierarchy.

    There should be same directory structure,./myowndir/myownsubdir/myownpackage directory forthe following package statement

    Package myowndir.myownsubdir.myownpackage;

  • 8/11/2019 Lecture 06 Java Classes and Packages

    10/26

    .

    package graphics;

    }

    name reflects the name of the package to

    .....\graphics\Rectangle.java

  • 8/11/2019 Lecture 06 Java Classes and Packages

    11/26

    Directory Structure of Java Source

    Files . , .

    files should be in a series of directories that

    Example : . pathname to source file: graphics/Rectangle.java .

  • 8/11/2019 Lecture 06 Java Classes and Packages

    12/26

    Directory Structure of Java Source

    Files However, the path to the .class files does not have to

    be the same as the path to the .java source files. Youcan arrange your source and class directoriesseparately, as:

    \sources\com\example\graphics\Rectangle.java\classes\com\example\graphics\Rectangle.class

    B doin this, ou can ive the classes director toother programmers without revealing your sources

    You also need to manage source and class files in this

    Machine (JVM) can find all the types your programuses

  • 8/11/2019 Lecture 06 Java Classes and Packages

    13/26

    su ose we lace the acka e schoolClasses under the C:

    directory. We need to set the classpath to point to that directory so that

    w en we ry o run , e w e a e o see w ere ourclasses are stored.

    we t e this at the command rom t

    C:\schoolClasses> set classpath=C:\ ,

    anywhere by typing,C:\schoolClasses> java schoolClasses.StudentRecord

  • 8/11/2019 Lecture 06 Java Classes and Packages

    14/26

    anywhere. You can also set more than one,

    ;(for windows)=

  • 8/11/2019 Lecture 06 Java Classes and Packages

    15/26

  • 8/11/2019 Lecture 06 Java Classes and Packages

    16/26

    Defining a new class based on an existing class is called

    The derived class is also called the direct subclass of thebase or su er class

    You can also derive classes from the derived class and soon

    Class A

    Class B

    Class C

  • 8/11/2019 Lecture 06 Java Classes and Packages

    17/26

    class B extends A{

    definition of class B

    }

    subclass of class A

    The class B can have additional members in addition to theinherited members of class A

  • 8/11/2019 Lecture 06 Java Classes and Packages

    18/26

    An inherited member of a base class is one that isaccessible within the derived class

    Base class members that are not inherited still form part ofa derived class object

    An inherited member of a derived class is a full member of

    that class and is freely accessible to any method in the

    Which members of the base class are inherited?

  • 8/11/2019 Lecture 06 Java Classes and Packages

    19/26

    The inheritance rules apply to class variables as well asinstance variables

    SubClasspu ic BaseC ass

    int a

    protected int c;

    private int d;

  • 8/11/2019 Lecture 06 Java Classes and Packages

    20/26

    You can define a data member in a derived class with the same nameas a a mem er n e ase c ass.

    The data member of the base class is still inherited but is hidden bythe derived class member with the same name

    The hiding will occur irrespective if the type or access modifiers are

    the same or not.ny use o t e er ve mem er name w a ways re er to t e mem er

    defined in derived class

    To refer to the inherited base class member, you must qualify it withthe keyword super

    Note that you cannot use super.super.something

  • 8/11/2019 Lecture 06 Java Classes and Packages

    21/26

    Methods in a base class excludin constructors are inherited in aderived class in the same way as the data members of the base class

    Methods declared as private in a base class are not inherited

    o e: ons ruc ors n e ase c ass are never n er e regar ess otheir attributes

    Though the base class constructors are not inherited in your derived,

    from your derived class constructor, the compiler will try to do it foryou

  • 8/11/2019 Lecture 06 Java Classes and Packages

    22/26

    If the first statement in a derived class constructor is not a call to aase c ass constructor, t e comp er w nsert a ca to t e e au t

    class constructor i.e super ( ), for youDefault call for base class constructor is with no arguments. Thissometimes result in a compiler error. Why?

    When you define your own constructor in a class, nodefault constructor is created by the compiler. Thus you

    yourself so that in a derived class you dont get thecompile error due to call to default constructor of baseclass

  • 8/11/2019 Lecture 06 Java Classes and Packages

    23/26

    Exam lepublic class Person {

    protected String name;protected String address;

    / * Default constructor */

    public Person() {System.out.println(Inside Person:Constructor)

    "" ""name = ; a ress = ;}. . . .

    }public class Student extends Person {public Student(){ System.out.println(Inside Student:Constructor);. . . .

    }

    public static void main ( String[] args ){=

    }

  • 8/11/2019 Lecture 06 Java Classes and Packages

    24/26

    A subclass can also explicitly call a constructor of its

    .

    This is done by using the super constructor call. A su er constructor call in the constructor of a subclass will

    result in the execution of relevant constructor from the super

    class, based on the arguments passed. ew t ngs to remem er w en us ng t e super constructor

    call: The su er call must occur as the first statement in a constructor The super() call can only be used in a constructor (not in ordinary

    methods)

  • 8/11/2019 Lecture 06 Java Classes and Packages

    25/26

    public Student(){super ome ame , ome ress ;

    System.out.println("Inside Student:Constructor");}

    Another use of super is to refer to members of the super class.

    public Student() {super.name = somename;super.address = some address;}

  • 8/11/2019 Lecture 06 Java Classes and Packages

    26/26

    Object class is mother of all classes

    In Java language, all classes are sub classed (extended)from the Object super class

    class

    Defines and im lements behavior common to all classes including the ones that you write

    getClass() equals() toString()