SEMINAR ON JAVA
INHERITANCE
THE MECHANISM
OF DERIVING A NEW
CLASS FROM AN OLD
ONE IS CALLED
INHERITANCE.
•THE OLD CLASS IS KNOWN
AS BASE CLASS OR SUPER
CLASS OR PARENT CLASS

THE NEW ONE IS CALLED
THE SUB CLASS OR DERIVED
CLASS OR CHILD CLASS
TYPES OF INHARITANCE
SINGLE INHERITANCE
         •ONLY
          ONE BASE
          CLASS
MULTILEVEL
        INHERITANCE
GRAND FATHER   SUPER CLASS


FATHER         INTERMEDIATE SUPERCLASS



CHILD          SUB CLASS
A DERIVED CLASS WITH MULTILEVEL BASE CLASSES IS DECLARED AS
FOLLOWS
CLASS A
{
…………
…………
}
CLASS B extends A // first level
{
………….
………….
}
CLASS C //second level
{
………….
………….
}
EXAMPLE
HERE CERTAIN
FEATURES OF ONE
LEVEL ARE SHARED
BY MANY OTHER
BELOW THE LEVEL.
MULTIPLE INHERITANCE
   OVERRIDING CAN BE DONE BY
    DEFINING A METHOD IN THE SUB
    CLASS THAT HAS THE SAME
    NAME,SAME ARGUMENTS AND
    SAME RETURN TYPE AS A
    METHOD IN THE SUPER CLASS.

   THIS IS KNOWN AS OVERRIDING.
FINALIZER METHODS
 FINILIZATION IS JUST OPPOSITE TO
 INITIALASATION.
 JAVA RUN-TIME IS AN AUTOMATIC
  GARBAGE COLLECTING SYSTEM.
 IT FREES THE MEMORY RESOURCES USED
  BY OBJECTS.
 BUT OBJECTS MAY HOLD OTHER NON-
  OBJECT RESOURCES SUCH AS FILE
  DESCRIPTORS OR WINDOW SYSTEM
  FONTS.
 THE GARBAGE COLLECTOR CANNOT FREE
  THESE RESOURCES.
 IN ORDER TO FREE THIS WE USE FINALIZER
  METHOD.
 THIS IS SIMILAR TO

 DESTRUCTORS.
 THE FINALIZER METHOD IS SIMPLY

 FINALIZE()
ABSTRACT METHODS
 FINALIZER
          HELPS US NOT TO RE-
 DEFINE THE METHOD IN SUB
 CLASS.

 BUT
    TO DO THE OPPOSITE, i.e.
 REDEFINE THE METHOD IN SUB
 CLASS,WE USE THE MODIFIER
 KEYWORD abstract IN THE
 METHOD DEFINITION
EXAMPLE

abstract class shape
{
………….
………….
abstract void draw();
………….
………….
}
VISIBILITY CONTROL
 IT
   IS ALSO KNOWN AS ACCESS
 MODIFIERS.THIS PROVIDES THREE
 TYPES OF MODIFIERS:

PUBLIC

PRIVATE

PROTECTED
By   simply declaring the variable or
 method as public it is visible to
 entire class in which it is defined.

A variable or method which is
 declared as public has the widest
 possible visibility and
 accessible everywhere.
FRIENDLY ACCESS
• When no access modifier is specified,the
  member defaults to a limited version of public
  accessibility known as “friendly” level of
  access.

• this makes fields visible only in the same
  package,but not in other packages.
   The visibility lies in between the public
    access and friendly access.

   The protected modifier makes the fields
    visible not only to all classes and sub
    classes in the same package but also to
    sub classes in other packages.
PRIVATE ACCESS
 They enjoy the highest   degree of protection.
 They are accessible only with their own class.
 They cannot      be accessed and inherited
 by sub classes.
 The method declared as private behaves like a
  method declared as final.
 It prevents the method from being sub classed.
A  field can be declared with two key
  words private and protected together like:
Private protected int codenumber;
 the visibility level is between the
  “protected” access and private access.
 This makes the fields visible in all
  subclasses regardless of what package
  they are in
   USE PUBLIC IF THE FIELD IS TO BE VISIBLE
    EVERYWHERE.
   USE PROTECTED IF THE FIELD IS TO BE
    VISIBLE EVERYWHERE IN THE CURRENT
    PACKAGE AND ALSO SUBCLASSES IN
    OTHER PACKAGES.
   USE “DEFAULT” IF THE FIELD IS TO BE
    VISIBLE EVERYWHERE IN THE CURRENT
    PACKAGE ONLY.
   USE PRIVATE PROTECTED IF THE FIELD IS
thank you

Seminar on java

  • 1.
  • 2.
  • 3.
    THE MECHANISM OF DERIVINGA NEW CLASS FROM AN OLD ONE IS CALLED INHERITANCE.
  • 4.
    •THE OLD CLASSIS KNOWN AS BASE CLASS OR SUPER CLASS OR PARENT CLASS THE NEW ONE IS CALLED THE SUB CLASS OR DERIVED CLASS OR CHILD CLASS
  • 5.
  • 6.
    SINGLE INHERITANCE •ONLY ONE BASE CLASS
  • 7.
    MULTILEVEL INHERITANCE GRAND FATHER SUPER CLASS FATHER INTERMEDIATE SUPERCLASS CHILD SUB CLASS
  • 8.
    A DERIVED CLASSWITH MULTILEVEL BASE CLASSES IS DECLARED AS FOLLOWS CLASS A { ………… ………… } CLASS B extends A // first level { …………. …………. } CLASS C //second level { …………. …………. }
  • 10.
  • 11.
    HERE CERTAIN FEATURES OFONE LEVEL ARE SHARED BY MANY OTHER BELOW THE LEVEL.
  • 12.
  • 13.
    OVERRIDING CAN BE DONE BY DEFINING A METHOD IN THE SUB CLASS THAT HAS THE SAME NAME,SAME ARGUMENTS AND SAME RETURN TYPE AS A METHOD IN THE SUPER CLASS.  THIS IS KNOWN AS OVERRIDING.
  • 14.
    FINALIZER METHODS  FINILIZATIONIS JUST OPPOSITE TO INITIALASATION.  JAVA RUN-TIME IS AN AUTOMATIC GARBAGE COLLECTING SYSTEM.  IT FREES THE MEMORY RESOURCES USED BY OBJECTS.  BUT OBJECTS MAY HOLD OTHER NON- OBJECT RESOURCES SUCH AS FILE DESCRIPTORS OR WINDOW SYSTEM FONTS.
  • 15.
     THE GARBAGECOLLECTOR CANNOT FREE THESE RESOURCES.  IN ORDER TO FREE THIS WE USE FINALIZER METHOD.  THIS IS SIMILAR TO DESTRUCTORS.  THE FINALIZER METHOD IS SIMPLY FINALIZE()
  • 16.
    ABSTRACT METHODS  FINALIZER HELPS US NOT TO RE- DEFINE THE METHOD IN SUB CLASS.  BUT TO DO THE OPPOSITE, i.e. REDEFINE THE METHOD IN SUB CLASS,WE USE THE MODIFIER KEYWORD abstract IN THE METHOD DEFINITION
  • 17.
  • 18.
    VISIBILITY CONTROL  IT IS ALSO KNOWN AS ACCESS MODIFIERS.THIS PROVIDES THREE TYPES OF MODIFIERS: PUBLIC PRIVATE PROTECTED
  • 19.
    By simply declaring the variable or method as public it is visible to entire class in which it is defined. A variable or method which is declared as public has the widest possible visibility and accessible everywhere.
  • 21.
    FRIENDLY ACCESS • Whenno access modifier is specified,the member defaults to a limited version of public accessibility known as “friendly” level of access. • this makes fields visible only in the same package,but not in other packages.
  • 22.
    The visibility lies in between the public access and friendly access.  The protected modifier makes the fields visible not only to all classes and sub classes in the same package but also to sub classes in other packages.
  • 23.
    PRIVATE ACCESS  Theyenjoy the highest degree of protection.  They are accessible only with their own class.  They cannot be accessed and inherited by sub classes.  The method declared as private behaves like a method declared as final.  It prevents the method from being sub classed.
  • 24.
    A fieldcan be declared with two key words private and protected together like: Private protected int codenumber;  the visibility level is between the “protected” access and private access.  This makes the fields visible in all subclasses regardless of what package they are in
  • 25.
    USE PUBLIC IF THE FIELD IS TO BE VISIBLE EVERYWHERE.  USE PROTECTED IF THE FIELD IS TO BE VISIBLE EVERYWHERE IN THE CURRENT PACKAGE AND ALSO SUBCLASSES IN OTHER PACKAGES.  USE “DEFAULT” IF THE FIELD IS TO BE VISIBLE EVERYWHERE IN THE CURRENT PACKAGE ONLY.  USE PRIVATE PROTECTED IF THE FIELD IS
  • 27.