You are on page 1of 3

PACKAGE Statements and import

Statements
The Package Statement should
be first statement of java
document. Other wise it is
Compile Error
A Class can't define in
multiple package statement
Import Statement should be
after the package statement
before class declarations
If Documents have classes
we cannot add package !Import
statements between class it
should be TP of the class
class "#$
%
m&'%(
(
package com.m)code*
import com.code$.+*
class "#
%
psvm&'
%
"#$.m&'*
"#other.m&'*
(
(
package com.m)code*
import com.code$.+*
class "#$
%
m&'%(
(
class "#
%
psvm&'
%
"#$.m&'*
"#other.m&'*
(
(
Class !eclarations
class ke"#ord should $e
simple ther #ise compile
error
Components
class ,c-
Access.odifier &public'
,O-
/on Access.odifier &0inal'
,O-
Class /ame ,C-
1ase class name&"#tend'
,O-
Implemented Interfaces ,O-
class bod) &%(' ,C-

,O- Optional ,C- Compulsor)


Classes and Interfaces can be
define an) order an) number
of occurrences in single 2ava
0ile
%ain %ethods
public static void main &String
#,-'
%
(
public static void main &String...
#'
%
(
public static void main &String
#...'
%
(
All of follo#ing main methods are
corrects
pu$lic static void main&String,- #'
pu$lic static void main&String ,-#'
pu$lic static void main&String #,-'
pu$lic static void main&String ,- #'
3ithout Import statements we
can call methods of other
package #ithin package name
class "#
%
psvm&'
%
"#$.m&'*
com.code$."#Other.m&'*
(
(
In Import statements we can4t
import same class name in
different packages
com.code$."#Other*
com.code."#Other*
class "#
%
psvm&'
%
(
(
Something like above program
we can add one import
statement other can $e called
#ithin package name
within + mark cant import
sub pacakges
import com&'(
within static import
statements we can import
pu$lic static
mem$ers&variables and
methods' of other class
directl)
import static com.code."#Other. marks*
To use all pu$lic static
members
import static com.code."#Other. **
)on Access modifier
A$stract
An abstract class ma) or
ma) not be declare
abstract methods
1ut for abstract methods
we need definitel)
abstract class
3e cannot create ob5ect
for abstract class
we have to inherit
abstract class to anther.
After inherit6s
If sub class is Concrete
class& It should be
definitel) override a$stract
methods Other wise it is sho#
Compile errors. *ut It is not
Complie "rror in Parent
a$stract class didn't have
a$stract methods
If sub class is a$stract
class& It is unnecessar"
override a$stract methods
Abstract ke)word cannot
use in variables
when we inheriting
abstract class6 &if it
have abstract methods' .it
should be have at least
one accessi$ilit" method
for parent class
+inal
0inal class can4t inherit
0inal variable can4t re,
assign value
class "#$%
final int a*
final int b78*
final int c*
public "#$&'
% a798 -- Correct (we can assign
first value in
constructor)
b798*!! wrong (assigned value 20
before)
(
public void m&'
%
c798!! wrong (we can assign
first value only in
constructor other wise we
have to guve value in final varible declaration)
( (

You might also like