By
Dr. Yasser Abdelhamid
Javaprogram structure
Packages
Constructors
Method declaration
Access Modifiers
Non-Access Modifiers
A package in Java is used to group related classes.
We use packages to avoid name conflicts, and to write
a better maintainable code.
The Java API is a library of prewritten classes
The library contains a large number of components
To use a class or a whole package from the library, you
need to use the import keyword
import java.util.Scanner;
// import java.util.*; to import the whole package
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
String userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
}
package mypack;
class MyPackageClass {
public static void main(String[] args) {
System.out.println("This is my
package!");
}
}
javac -d . MyPackageClass.java
java mypack.MyPackageClass
Create a class named “Employee”, that
has the attributes: name, address,
department, id , phone number, and basic
salary.
Create the following methods:
netSal : to calculate net salary which is 80% of
the basic salary.
dispEmp: to display all information of the
employee on console.
Create
the class “TestClass “ to test the
Employee class.
A constructor in Java is a special method that is used
to initialize objects.
public class Class1{
int x;
public Class1(int y) {
x = y;
}
public static void main(String[] args) {
Class1 myObj = new Class1(5);
System.out.println(myObj.x);
}
}
The constructor is called when an object of a
class is created.
It can be used to set initial values for object
attributes
public class Class1{
int x;
public Class1(int y) {
x = y;
}
public static void main(String[] args) {
Class1 myObj = new Class1(5);
System.out.println(myObj.x);
}
}
The constructor name must match the class
name, and
It cannot have a return type.
Constructors can take parameters, which are
used to initialize attributes.
If you do not create a class constructor
yourself, Java creates a default constructor
that does not make any initializations.
Modifiersare keyword used to control the
access or functionality of classes,
attributes, methods and constructors.
They are classified into:
Access Modifiers - controls the access level
Non-Access Modifiers - do not control access
level, but provides other functionality
public : The class is accessible by any
other class.
default : The class is only accessible by
classes in the same package. This is used
when you don't specify a modifier.
public class MyPublicClass{
public static void main(String[] args) {
System.out.println(“I am accessible to any other class");
}
}
class MyDefaultClass{
public static void main(String[] args) {
System.out.println(“I am accessible only
to classes in the same package");
}
}
public : The code is accessible for all
classes
private : The code is only accessible
within the declared class
default : The code is only accessible in the
same package. This is used when you
don't specify a modifier.
protected : The code is accessible in the
same package and subclasses.
final:
The class cannot be inherited by other
classes
abstract
The class cannot be used to create objects.
To access an abstract class, it must be
inherited from another class.
final
Attributes and methods cannot be
overridden/modified.
static
Attributes and methods belongs to the class,
rather than an object
abstract
Can only be used in an abstract class
static method, can be accessed without creating an object of
the class.
public methods, can only be accessed through creating
objects.
public class Class01 {
static void myStaticMethod() {
System.out.println("I am a static method");
}
public void myPublicMethod() {
System.out.println("I am a public method");
}
public static void main(String[] args) {
Class01.myStaticMethod();
Class01 obj1 = new Class01();
obj1.myPublicMethod();
}
}