0% found this document useful (0 votes)
13 views17 pages

Lecture 04 PART II JAVA

The document provides an overview of Java modifiers, categorizing them into access and non-access modifiers, detailing their functionalities and usage. It explains access modifiers like public, private, and protected, as well as non-access modifiers such as final, static, and abstract. Additionally, it covers Java classes, constructors, methods, parameters, and inner classes, illustrating concepts with code examples to demonstrate their application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

Lecture 04 PART II JAVA

The document provides an overview of Java modifiers, categorizing them into access and non-access modifiers, detailing their functionalities and usage. It explains access modifiers like public, private, and protected, as well as non-access modifiers such as final, static, and abstract. Additionally, it covers Java classes, constructors, methods, parameters, and inner classes, illustrating concepts with code examples to demonstrate their application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

LECTURE-04 – PART-II

Java Modifiers
access modifier, meaning that it is used to set the access level for classes, attributes, methods and constructors.

We divide modifiers into two groups:


•Access Modifiers - controls the access level
•Non-Access Modifiers - do not control access level, but provides other functionality

Access Modifiers

For classes, you can use either public or default: For attributes, methods and constructors, you
can use the one of the following:
Modifier Description
Modifier Description
public The code is accessible for all classes
public The class is accessible by any other
class private The code is only accessible within the
declared class
default The class is only accessible by classes
in the same package. This is used default The code is only accessible in the same
when you don't specify a modifier. package. This is used when you don't specify
a modifier.

protected The code is accessible in the same package


and subclasses.
Non-Access Modifiers
For classes, you can use
either final or abstract: For attributes and methods, you can use the
Modifier Description one of the following:
final The class cannot be inherited by other Modifier Description
classes final Attributes and methods cannot be
overridden/modified
abstract The class cannot be used to create objects
(To access an abstract class, it must be static Attributes and methods belongs to the class, rather
inherited from another class. than an object
abstract Can only be used in an abstract class, and can only
be used on methods. The method does not have a
body, for example abstract void run();. The body is
provided by the subclass (inherited from).

transient Attributes and methods are skipped when


serializing the object containing them
synchronized Methods can only be accessed by one thread at a
time
volatile The value of an attribute is not cached thread-
locally, and is always read from the "main memory"
Final Static
If you don't want the ability to override existing A static method means that it can be accessed without creating an
attribute values, declare attributes as final: object of the class, unlike public:

Example Example
An example to demonstrate the differences
public class Main { between static and public methods:
final int x = 10;
final double PI = 3.14; public class Main {
public static void main(String[] args)
{ static void myStaticMethod() {
Main myObj = new Main(); System.out.println("Static methods can be called without
myObj.x = 50; creating objects"); }
myObj.PI = 25; // will generate an
error: cannot assign a value to a final public void myPublicMethod() {
variable System.out.println("Public methods must be called by creating
System.out.println(myObj.x); objects"); }
}
} public static void main(String[ ] args) {

myStaticMethod();
myPublicMethod();
Main myObj = new Main();
myObj.myPublicMethod();

} }
Abstract
An abstract method belongs to an abstract class, and it does not have a body. The body is provided by the subclass:

abstract class Main


{
public String fname = "John";
public int age = 24;
public abstract void study(); // abstract method }

// Subclass (inherit from Main)


class Student extends Main {
public int graduationYear = 2018;
public void study() {
// the body of the abstract method is provided here
System.out.println("Studying all day long"); } }
// End code from filename: Main.java

// Code from filename: Second.java


class Second {
public static void main(String[] args) {
// create an object of the Student class (which inherits attributes and methods from
Main)
Student myObj = new Student();
System.out.println("Name: " + myObj.fname);
System.out.println("Age: " + myObj.age);
System.out.println("Graduation Year: " + myObj.graduationYear);
myObj.study(); // call abstract method
} }
Java Class and Object Multiple Objects
Example
You can create multiple objects of one
Create a class Main and an object class:
called "myObj" and print the value of Example
x:
public class Main { Create two objects of Main:

public class Main {


int x = 5; int x = 5;
public static void main(String[] args)
public static void main(String[] args) {
{ Main myObj1 = new Main(); // Object 1
Main myObj2 = new Main(); // Object 2
Main myObj = new Main(); System.out.println(myObj1.x);
System.out.println(myObj2.x);
System.out.println(myObj.x); }
} }
}
Using Multiple Classes
You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class
has all the attributes and methods, while the other class holds the main() method (code to be executed)).
Remember that the name of the java file should match the class name. In this example, we have created two files in the same
directory/folder:

•Main.java
Main.java
•Second.java
public class Main
{ Second.java
class Second
{
int x = 5;
public static void main(String[]
args)
{
}
Main myObj = new Main();
System.out.println(myObj.x);

}
Accessing Modify override existing
Attributes Attributes values:
You can access attributes by creating You can also modify attribute
an object of the class, and by using values:
the dot syntax (.):
The following example will create an
Example
object of the Main class, with the
Example
name myObj. We use the x attribute on
Change the value of x to 25:
the object to print its value: Set the value of x to 40:
public class Main { int x = 10;
Example public class Main {
public static void main(String[] args)
Create an object called "myObj" int x;
and print the value of x: {
public static void main(String[]
public class Main args) Main myObj = new Main();
{ {
int x = 5; myObj.x = 25; // x is now 25
public static void main(String[] Main myObj = new Main();
args) System.out.println(myObj.x);
{ myObj.x = 40;
System.out.println(myObj.x); }
Main myObj = new Main(); The final keyword is useful when
System.out.println(myObj.x); } you want a variable to always store
} } the same value, like PI (3.14159...).
} }
Java Class Methods : methods are declared within a class, and that they are used to perform
certain actions:

Example
Create a method named myMethod() in
Main:
And Inside main, call myMethod():

public class Main


{

static void myMethod()


{
System.out.println("Hello World!");
 A method is a block of code which only runs
}
when it is called.
public static void main(String[] args)
{  You can pass data, known as parameters, into a
method.
myMethod();
 Methods are used to perform certain actions,
} } // Outputs "Hello World!"
and they are also known as functions.
Parameters and
Arguments
Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a
comma.

Multiple Parameters
Example
public class Main { You can have as many parameters as you
static void myMethod(String fname) like:
{
System.out.println(fname + " Example
Refsnes"); public class Main {
}
public static void main(String[] static void myMethod(String fname, int age)
args) {
{ System.out.println(fname + " is " + age);
myMethod("Liam");
myMethod("Jenny"); }
myMethod("Anja"); } }
public static void main(String[] args)
{
myMethod("Liam", 5);
myMethod("Jenny", 8);
myMethod("Anja", 31);
}
}
Static vs. Public
You will often see Java programs that have either static or public attributes and methods.In the example above, we created
a static method, which means that it can be accessed without creating an object of the class, unlike public, which can only be
accessed by objects:

Example
An example to demonstrate the differences between static and public methods:

public class Main {


// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects"); } // Public method

public void myPublicMethod() { System.out.println("Public methods must be called by creating


objects"); } // Main method
public static void main(String[] args)
{ myStaticMethod(); // Call the static method // myPublicMethod(); This would compile an error
Main myObj = new Main(); // Create an object of Main
myObj.myPublicMethod(); // Call the public method on the object
} }
Java Constructors
A constructor in Java is a special method that is used to initialize objects. The constructor is
called when an object of a class is created. It can be used to set initial values for object
attributes:

Example
Create a constructor:
// Create a Main class
public class Main  Note that the constructor name must match the
{
int x;
class name, and it cannot have a return
public Main() { x = 5; type (like void).
public static void main(String[] args)
{
 Also note that the constructor is called when the
Main myObj = new Main();
System.out.println(myObj.x); object is created.

} } // Outputs 5
 All classes have constructors by default: if you do
not create a class constructor yourself, Java creates
one for you. However, then you are not able to set
initial values for object attributes.
Constructor Parameters
Constructors can also take parameters, which is used to initialize attributes.
The following example adds an int y parameter to the constructor. Inside the constructor we set x to y (x=y). When we call the
constructor, we pass a parameter to the constructor (5), which will set the value of x to 5:

Example Example
public class Main { public class Main {
int modelYear;
int x; String modelName;
public Main(int y) { public Main(int year, String name) {
x = y; modelYear = year;
} modelName = name; }
public static void main(String[]
args)
{ public static void main(String[] args) {
Main myObj = new Main(5); Main myCar = new Main(1969, "Mustang");
System.out.println(myObj.x); System.out.println(myCar.modelYear + " " +
} } myCar.modelName);
// Outputs 5 } } // Outputs 1969 Mustang
Java Inner Classes
In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes
that belong together, which makes your code more readable and maintainable. To access the inner class, create an
object of the outer class, and then create an object of the inner class:

Example
class OuterClass {
int x = 10;

class InnerClass {
int y = 5;
} }

public class Main {


public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new
InnerClass();
System.out.println(myInner.y + myOuter.x);
} }
// Outputs 15 (5 + 10)
Private Inner Class
Unlike a "regular" class, an inner class can be private or protected. If you don't want outside objects to
access the inner class, declare the class as private:

Example
class OuterClass {
int x = 10;
private class InnerClass {
int y = 5;
} }

public class Main {


public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
} }
Static Inner Class
An inner class can also be static, which means that you can access it without creating an object of the outer class:

Example
class OuterClass {
int x = 10;
static class InnerClass {
int y = 5;
} }

public class Main {

public static void main(String[] args) {

OuterClass.InnerClass myInner = new


OuterClass.InnerClass();

System.out.println(myInner.y);

} } // Outputs 5

Note: just like static attributes and methods, a static inner class does not have access to members of the outer class.
Access Outer Class From Inner Class
One advantage of inner classes, is that they can access attributes and methods of the outer class:

Example
class OuterClass {
int x = 10;

class InnerClass {

public int myInnerMethod() {

return x; } } }

public class Main {

public static void main(String[] args) {

OuterClass myOuter = new OuterClass();

OuterClass.InnerClass myInner = myOuter.new InnerClass();

System.out.println(myInner.myInnerMethod());

} } // Outputs 10

You might also like