VB_Lecture_3
VB_Lecture_3
2. final
3. abstract
4. synchronized
5. transient
6. volatile
7. Strictfp
Final Keyword
⚫ Java final keyword is a non-access specifier
that is used to restrict a class, variable, and
method.
⚫ If we initialize a variable with the final keyword, then
we cannot modify its value.
⚫ If we declare a method as final, then it cannot be
overridden by any subclasses.
⚫ If we declare a class as final, we restrict the other
classes to inherit or extend it. In other words, the final
classes can not be inherited by other classes.
Final Keyword
Final Variables in Java
• The normal variable and final variable differ only by one parameter that we can
change the value of a normal variable once assigned, but we cannot change the
value of a final variable once assigned.
• Therefore, we must use the final variables only for the assigning values that we
want to remain constant throughout the execution of the program.
Final Keyword
Final Methods in Java Compile Time Errors are those
errors which prevent the code
from running because of an
incorrect syntax such as a
missing semicolon at the end of
a statement or a missing
bracket, class not found
A static class can access only the static members of the outer class
Static Keyword
Static class in Java
class JavaExample{
private static String str = "BeginnersBook";
static class MyNestedClass{
public void disp() {
/* If you make the str variable of outer class, non-static then you will get compilation
error because: a nested static class cannot access non- static members of the outer class */
System.out.println(str); } }
public static void main(String args[]){
/* To create instance of nested class we didn't need the outer class instance
but for a regular nested class you would need to create an instance of outer class
first */
JavaExample.MyNestedClass obj = new JavaExample.MyNestedClass();
obj.disp(); } }
Strictfp Keyword
• By default, the floating-point computations in Java are
platform-dependent. And so, the floating-point outcome's
precision depends on the hardware in-use.
• Strictfp is used in java for restricting floating-point
calculations and ensuring the same result on every platform
while performing operations in the floating-point variable.
Strictfp Keyword
strictfp modifier is used with classes, interfaces, and
methods only but is not applicable to apply with abstract
methods, variables or constructors.
⚫ class – All code in the class (instance, variable, static
initializers), and code in nested classes will use strictfp
computations.
⚫ method – All code within method will use strictfp
computations.
⚫ interface – All code in any class that implements the
interface will use strictfp computations.
Strictfp Keyword
strictfp modifier is used with classes, interfaces, and
methods only but is not applicable to apply with abstract
methods, variables or constructors.
strictfp class A{} //strictfp applied on class
strictfp interface M{} //strictfp applied on interface
class A{ strictfp void m(){} //strictfp applied on method }
class B{ strictfp abstract void m(); //Illegal combination of
modifiers }
class B{ strictfp int data=10; //modifier strictfp not allowed
here }
class B{ strictfp B(){} //modifier strictfp not allowed here }
Transient Keyword
⚫ The transient keyword in Java is used to avoid
serialization.
⚫ Examples: Customer SSN or password need not to be stored.
⚫ Serialization is the process of converting an object into a
byte stream, and deserialization is the opposite of it.
⚫ When we mark any variable as transient, then that
variable is not serialized.
⚫ Since transient fields aren't present in the serialized form
of an object, the deserialization process would use the
default values for such fields when creating an object
out of the serialized form.
Transient Keyword
Transient Keyword
Transient Keyword
Notes:
⚫ Transient keyword or modifier is applicable only
for variables
⚫ transient and static: Since static fields are not
part of state of the object, there is no use/impact
of using transient keyword with static variables.
However there is no compilation error.
⚫ transient and final: final variables are directly
serialized by their values, so there is no
use/impact of declaring final variable as transient.
There is no compile-time error though.
Recap – OOP Concepts
⚫ Polymorphism is derived from two Greek
words: poly and morphs. The word “poly”
means many and “morphs” means forms. So,
polymorphism means many forms.
2. Method Overloading
⚫ When we have one or more methods with the
same name and/or return types but different
parameter lists (different signatures)
▪ void func() { ... }
▪ float func(double a) { ... }
▪ float func(int a, float b) { ... }
⚫ This is an example of compile time (static
polymorphism or early binding)
Recap – OOP Concepts
⚫ We can achieve polymorphism in Java using the
following ways:
1. Method Overriding
2. Method Overloading
⚫ The overloaded methods must have different
method signatures.
▪ It means that the methods must differ in at least one
of these: number of parameters, type of parameters,
and order of parameters, but should have the
same name
▪ The return type of overloaded methods may or
may not have the same return type
Polymorphism - Overloading
Polymorphism - Overloading
If we have a restaurant deals with MAC and KFS, so we
will have different type of the order.
Polymorphism - Overloading
Advantages of method overloading
⚫ Increases the readability of the program
⚫ Example:
⚫ Instead of implementing order1, order 2, and order 3
Polymorphism - Overloading
Advantages of method overloading
⚫ Increases the readability of the program
⚫ Example:
⚫ Add contact to the list in mobile phone application
Polymorphism
Can we override a static method?
Polymorphism
Can we override a static method?
NO
Note:
Upcasting!
Upcasting and Downcasting in
Java
⚫ A process of converting one data type to another is
known as Typecasting.
⚫ Upcasting and Downcasting is the type of object
typecasting.
⚫ Upcasting
⚫ The process of casting an object of child class to the parent class
is called Upcasting.
⚫ Upcasting can be done implicitly or explicitly.
⚫ Upcasting will reduce the choice of methods, we can use on the
type casted object.
⚫ However, for overridden methods, static or dynamic binding
comes into play, and it will decide the method implementation to
use.
Upcasting and Downcasting in
Java
class SuperClass
{
public void print()
{ System.out.println("Printing from SuperClass");}
}
class Child extends SuperClass
{
String name;
Child(String s){ this.name = s; }
@Override
public void print() { System.out.println("Printing from Child class");}
⚫ Control Abstraction
Error
Abstraction - Example
Solution
Abstraction - Example
Abstract class is written in
italic format in UML class
diagram
Swing - Inheritance Hierachy
Swing - Containers
1. JFrame
2. JPanel: it is a container that can be used in
both applications and applets
⚫ Panel, like all containers, can contain both visual
components and other containers.
3. JApplet: It is a special type of program that
is embedded in the webpage to generate the
dynamic content. It runs inside the browser
and works at client side.
Jpanel vs. JFrame
JPanel JFrame
Parent class javax.swing.JComponent java.awt.Frame
What is it? A specific area for A window for hosting
putting in GUI standalone
components and applications, like an
operations. alert window or
notification window.
Title Bar There is no title bar. It contains a title bar.
Weight Light Heavy
Contain Multiple GUI It is the window; it can
components and have one or more
operations, but isn’t a JPanel instances
window. inside it
JFrame - Example
FrameDemo.Java
NewClass.Java
JPanel - Example
FrameDemo.Java
NewClass.Java
JPanel - Example
Swing - Examples
Question: Write a program that creates five instances of
JButtons dynamically using the inheritance concept. The
output of the program must be as follows.
Hint
The window size is 400x400
Answer
FrameDemo.Java
Answer
NewClass.Java
Swing - Examples
Question: Write a program that creates 15 instances of
JButtons dynamically using the inheritance concept. The
output of the program must be as follows.
Hint
The window size is 400x400
Answer
FrameDemo.Java
NewClass.Java
Swing - Layouts
Do we always have to use layout managers?
No, it is also possible not to use any layout manager and to
place components manually. This is known as absolute
positioning.
To convert to absolute positioning, it is done in 3 steps;
1. Specify the layout manager to be null
NewClass.Java
JPanel - Example
Question: What is the output of the following program?
Answer
Question: What is the output of the following program?
JPanel - Example
Question: What is the output of the following program?
Answer
Question: What is the output of the following program?
81