Docstoc

Object Oriented Programming in Java

Document Sample
Object Oriented Programming in Java Powered By Docstoc
					                   Lecture 3
     Object Oriented Programming in Java

                       Language Basics
               Classes, Interfaces and Packages




June 1, 2000         Object Oriented Programming in Java (95-707)   1
                                  Java Language Basics
                   Assignment B

• Primitive objects and primitive object wrapper
  classes
• throwing exceptions




June 1, 2000   Object Oriented Programming in Java (95-707)   2
                            Java Language Basics
                Today’s Lecture

• Trail: Learning the Java Language
• Lesson: Classes, Interfaces, and Packages




June 1, 2000   Object Oriented Programming in Java (95-707)   3
                            Java Language Basics
               What makes up a class:

•   class declaration
•   class body
•   constructor for the class
•   variables
•   methods




June 1, 2000      Object Oriented Programming in Java (95-707)   4
                               Java Language Basics
               Inheritance Tree

• An object that is instantiated from a class
  contains all the variables and methods defined
  for that that class and all its ancestors.
• However, the methods may have been
  modified (overridden) somewhere along the
  way. Also, access to those variables and
  methods may have been restricted through
  use of the public, private, and protected
  keywords




June 1, 2000   Object Oriented Programming in Java (95-707)   5
                            Java Language Basics
                    The parents rule

• Java does not involve a specification of an
  access specifier, public, private, or protected
  at the inheritance interface. In other words,
  inheritance access control cannot be used in
  Java to modify the access control assigned to
  a member of the parent class:
      – in other words (english) once the parent defines the
        access to its methods and variables, the children
        cannot modify them.




June 1, 2000       Object Oriented Programming in Java (95-707)   6
                                Java Language Basics
                           Inheritance

• extends keyword
• Default superclass: Object
      – Inheriting the methods of the Object class
      – inheriting the variables of the Object class
• class A inherits from class B
      – class A inherits the variables from class B
      – class A inherits the methods from class B
• Inheritance is restricted to the non-private
  methods and variables




June 1, 2000        Object Oriented Programming in Java (95-707)   7
                                 Java Language Basics
               Examples of inheritance




June 1, 2000      Object Oriented Programming in Java (95-707)   8
                               Java Language Basics
               Public, Final & Abstract classes

• Classes can be declared to be either public,
  abstract, or final, or some combination of
  the three.
• However, before we can make any sense out
  of the public keyword, we need to know a
  little about how classes are grouped into
  packages.




June 1, 2000          Object Oriented Programming in Java (95-707)   9
                                   Java Language Basics
                            Packages

• We will see packages later on in more details.
• In simple words, packages are a way to group
  classes together.
• A class defines its package by adding the
  keyword “package” at the beginning of the
  file.
• Example:
      – package com.mellon.aPackageName




June 1, 2000      Object Oriented Programming in Java (95-707)   10
                               Java Language Basics
                   Public classes

• Explicitly define the public keyword otherwise
  your class will be visible only within its own
  package




June 1, 2000   Object Oriented Programming in Java (95-707)   11
                            Java Language Basics
                Abstract Classes

• An abstract class is a class that is designed to
  be inherited from (subclassed). It is not
  intended to be a class from which objects are
  instantiated (you cannot do a “new” on the
  class)
• Note that the methods themselves can be
  abstract
• Why create abstract classes?




June 1, 2000   Object Oriented Programming in Java (95-707)   12
                            Java Language Basics
                         Final Classes

• The opposite of an abstract class is a final
  class. A final class cannot be subclassed.
• Why use final classes?
• Can a class be both abstract and final?
• Examples:
      – abstract and final
      – final class A is extended by class B




June 1, 2000        Object Oriented Programming in Java (95-707)   13
                                 Java Language Basics
     Examples of abstract and final classes




June 1, 2000   Object Oriented Programming in Java (95-707)   14
                            Java Language Basics
                        Interfaces

• Interfaces are like the roles that a class can
  play
• A class that implements an interface must
  define the methods that are specified by the
  interface
• Whenever a class claims to implement an
  interface, you can be assured that it provides
  a definition for all the methods declared within
  that interface. Otherwise, the class cannot
  be compiled.



June 1, 2000   Object Oriented Programming in Java (95-707)   15
                            Java Language Basics
               Implementing Interfaces

• A class may implement one or more interfaces
  using the keyword implements and a
  comma-delimited list of interface names as
  shown below:

    class MyClassName extends MySuperClass
                    implements MyInterface, YourInterface
    {
        //body of class
    }




June 1, 2000      Object Oriented Programming in Java (95-707)   16
                               Java Language Basics
               Examples with interfaces




June 1, 2000      Object Oriented Programming in Java (95-707)   17
                               Java Language Basics
                    Constructors

• Constructors are used to instantiate a class
• Even if you don’t explicitly define a constructor
  the compiler creates one for you
• It is safer to explicitly define the
  constructor(s)
• Constructor(s) are methods that have the
  same name has the class name and return
  void




June 1, 2000   Object Oriented Programming in Java (95-707)   18
                            Java Language Basics
               Multiple Constructors

• A class can have multiple constructors
• The only difference between them is the list of
  parameters that comes with the constructor
• Why are they parameters to the constructors?




June 1, 2000    Object Oriented Programming in Java (95-707)   19
                             Java Language Basics
               A “super” keyword

• The super keyword allows a constructor to
  call the constructor of a parent class
• If present in the constructor the super
  keyword must appear first in the body of the
  method
• Why must super appear first?




June 1, 2000   Object Oriented Programming in Java (95-707)   20
                            Java Language Basics
          Access control to the constructors

• A constructor can be private, protected, or
  public
• Always explicitly specify the access
• If all the constructors are private how do I
  instantiate the class




June 1, 2000      Object Oriented Programming in Java (95-707)   21
                               Java Language Basics
               Examples of constructors




June 1, 2000      Object Oriented Programming in Java (95-707)   22
                               Java Language Basics
                                     Variables

• Types of variables
      – Member variables
               • class variables
               • Instance variables
      – Method variables
               • local variables




June 1, 2000               Object Oriented Programming in Java (95-707)   23
                                        Java Language Basics
                The “static” keyword

• The keyword static determines whether the
  variable is a class variable or an instance
  variable
• To access a static variable you must qualify it
  with the class name (NOT the instance name)
      – for example:
        ClassName.theVariableName = aValue;
• Why should we use static variable?




June 1, 2000      Object Oriented Programming in Java (95-707)   24
                               Java Language Basics
                       Final Variables

• Determines whether the variable is a constant
  or not
• Example:
      – declaring a final object and trying to assign a new
        value to it




June 1, 2000        Object Oriented Programming in Java (95-707)   25
                                 Java Language Basics
   Controlling access to member variables

• Classes can protect their member variables
  from being access by other objects
               Specifier   class           subclass          package      world



               private     X




               protected   X               X*                X




               public      X               X                 X            X




               package     X                                 X




June 1, 2000               Object Oriented Programming in Java (95-707)           26
                                        Java Language Basics
         Subtleties of the private keywords

• It is applied at the class level. In other words,
  objects of the same types have access to each
  other’s private variables




June 1, 2000     Object Oriented Programming in Java (95-707)   27
                              Java Language Basics
       Subtleties of the protected keyword

• The protected member variable of a super
  class is accessible from a subclass only if the
  subclass and the superclass are in the same
  package




June 1, 2000    Object Oriented Programming in Java (95-707)   28
                             Java Language Basics
               Subtleties of the public keyword

• None!
• Everyone can see a public member variable.




June 1, 2000          Object Oriented Programming in Java (95-707)   29
                                   Java Language Basics
         Subtleties of the package keyword

• You get package level access if you don’t
  specify the access level




June 1, 2000     Object Oriented Programming in Java (95-707)   30
                              Java Language Basics
     Examples of access levels on member
                  variables




June 1, 2000   Object Oriented Programming in Java (95-707)   31
                            Java Language Basics
         Access level to member methods

• Works just like member variables access level




June 1, 2000    Object Oriented Programming in Java (95-707)   32
                             Java Language Basics
               Instance Vs. Class Members
                 (methods and variables)

• “instance” member by default
• use “static” to override the default and it the
  member becomes a “class” member




June 1, 2000        Object Oriented Programming in Java (95-707)   33
                                 Java Language Basics
                 Class Members

• All instances of a class share the same copy of
  the class member
• class variables can be accessed via the
  instance (use the instance name) or via the
  class itself (use the class name)




June 1, 2000   Object Oriented Programming in Java (95-707)   34
                            Java Language Basics
               Instance Members

• By default all members are instance members
• Every instantiated class carries its own copy of
  the instance variable. In other words, instance
  variables are not shared across instances of
  the class




June 1, 2000   Object Oriented Programming in Java (95-707)   35
                            Java Language Basics
 Examples of instance and class members




June 1, 2000   Object Oriented Programming in Java (95-707)   36
                            Java Language Basics
                                    Packages

• Purpose:
      – grouping relevant classes together
      – avoiding name clashes among developers
• Notation:
               package mypackage
               public class AnyClass {
               ….
               }
• Scope of the package statement:
      – entire source file
• If you don’t declare a package:
      – the compiler puts your class in the default package

June 1, 2000              Object Oriented Programming in Java (95-707)   37
                                       Java Language Basics
                  Package name conventions

• Use reversed Internet domain name:
      – com.company.package
      – example:
               • com.mellon.lineofbusiness.packagename
               • com.sun...
               • edu.cmu…




June 1, 2000             Object Oriented Programming in Java (95-707)   38
                                      Java Language Basics
                          Using a package

• Strategy 1:
      – use the packagename.classname approach
      – example:
                     » com.mellon.aClassName s = new com.mellon.aClassName();

• Strategy 2
      – import the package
      – put import statement at the beginning of the file and
        after the package statement:
               import com.mellon.*;
               …
               aClasName s = new aClassName();
      – Use wildcards where necessary
• java.lang.* is always imported by default
June 1, 2000             Object Oriented Programming in Java (95-707)   39
                                      Java Language Basics
          Compiler, Run-time and Packages

• Compiler (javac):
      – creates directories that reflect the package hierarchy
      – compiles the files and places them in their respective
        directory hierarchy
      – uses the “classpath” option on the command line to
        find the source files within the hierarchy of
        directories.
      – Example:
                » javac -classpath .;c:\myclasses;c:\jdk\lib\classes.zip *.java

• Run-time (java):
      – uses the “classpath” option on the command line to
        find classes within the hierarchy of directories.
      – Example:
                » java -classpath .;c:\myclasses;c:\jdk\lib\classes.zip

June 1, 2000        Object Oriented Programming in Java (95-707)          40
                                 Java Language Basics

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:15
posted:8/10/2011
language:English
pages:40