0% found this document useful (0 votes)
29 views35 pages

Day7 - Static Abstract and Final

Uploaded by

mohamedjaaved19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views35 pages

Day7 - Static Abstract and Final

Uploaded by

mohamedjaaved19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Static,Final,Abstract Keyword

TNS India Foundation | ‹#›


Partners in Economic Transformation
Recap

● Inheritance - Creating new classes based on existing ones.


● Types of Inheritance- Single ,Multilevel,Hybrid & Hierarchical.
● this Keyword -Refer to the current object of the method or constructor.
● Instance of - To test whether the object is an instance of the specified type.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. In Java, can a subclass inherit constructors from its superclass?


A Only if the subclass is marked as “final”

B Yes, a subclass inherits constructors from its superclass

C
Only if the superclass is marked as "static"
D
Only if the subclass is marked as "final"

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. In Java, what does the "this" keyword refer to?

A It refers to the current instance of the class

B It refers to the super class of the current class

C
It refers to the current method being executed.
D
It refers to the current object in the entire program.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Learning Objective

● Understanding the Concept of the Static Keyword, Static Block, and Static Method.
● Exploring the application of the static keyword, block, and method through
illustrative examples and comprehending their advantages.
● Understanding the Significance of the Final Keyword.
● Examining examples and elucidating the advantages associated with utilizing the
final keyword in programming contexts.
● Understanding the Essence of the Abstract Keyword.
● Examining examples and outlining the advantages of employing the abstract
keyword in programming paradigms.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hook

What are the main things as programmer has to focus while coding?

TNS India Foundation | ‹#›


Partners in Economic Transformation
Static

● Static is a keyword that is used for memory management mainly.


● Static means single copy storage for variables or methods.
● The members that are declared with the static keyword inside a class are called static
members in java.
● These members can be accessed even if no instance of the class exists because static
members are not tied to a particular instance.
● They are shared across all instances of the class.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Features Of Static Keyword

● Static keyword in Java can be applied with variables, methods, inner classes, and
blocks.
● We cannot declare a class with static keyword but the inner class can be declared as
static.
● One basic rule of working with static keyword is that we cannot directly call instance
members within the static area because the static members are linked with the class.
● Static members get memory once when the class is loaded into the memory.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Static Variable

● A static variable can be accessed inside any other class using the class name.
● Syntax :

A.x; // A is the class name and "x " is a static variable declared in that class.

● It can be accessed and modified by any other objects of class.


● A static variable can be directly accessed by class name within static and non-static
methods of the class.
● It can be used with public and final modifiers to make constant value.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Static Method

● Static method is also tied to the class, not to an object of class.

● It can be called and executed without creating object of the class.

● Static method can be called or accessed directly using class name.


● Syntax :
● className.methodName(); // Here, className is the name of a class.
● Example:
Student.add(); // Student is the name of class and add is a method.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Features of Static method

● A static method in a class can directly access other static members of the class.
● It cannot access instance (i.e. non-static) members of a class.
● An instance method can call an instance or static method. It can also access instance
or static data variable.
● A static method can call a static method only. It can only access a static data variable
inside the static method.A static method cannot invoke an instance method or access
an instance variable.
● Static (variable, method, and inner class) are stored in Permanent generation memory
(class memory).

TNS India Foundation | ‹#›


Partners in Economic Transformation
Features of Static method

● We cannot declare a static method and instance method with the same signature in the
same class hierarchy.
● When we create a static method in the class, only one copy of the method is created in
the memory and shared by all objects of the class. Whether you create 100 objects or 1
object.
● A static method in java is also loaded into the memory before the object creation.
● The static method is always bound with compile time.
● It cannot refer to this or super in any way.
● Static methods can be overloaded in Java but cannot be overridden because they are
bound with class, not instance.
TNS India Foundation | ‹#›
Partners in Economic Transformation
TNS India Foundation | ‹#›
Partners in Economic Transformation
Static Block

● A block is declared with the static keyword, it is called static block in Java.
● It is a normal block of code that is enclosed in braces ({ }) and is preceded by a
keyword “static”.
● A static block is also known as static initialization block or static initializer block in Java.

● Syntax:
static
{
// Logic here or Java code.
}
TNS India Foundation | ‹#›
Partners in Economic Transformation
Static Block Execution

TNS India Foundation | ‹#›


Partners in Economic Transformation
Use of Static Block,static Method

● We mainly use the static block to initialize static variables, set up resources, or perform
any other tasks that need to be done before the class is used.
● static methods is when we reuse standard behavior across instances of different
classes.

● Static variables are used to keep track of information that relates logically to an entire
class.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Real Time application

Flight ticket counter!


The total number of seats in a flight are static, the number gets updated for every user while
booking the seats, every user will see the updated value of available seats.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Final Keyword

The final keyword declared with variable, method, and class indicates that “This cannot be
modified”.
● The value of the final variable cannot be changed.
● A final method cannot be overridden.
● A final class cannot be inherited.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Use of Final Keyword

● The first use is to prevent inheritance, as the final classes cannot be extended.
● The second use is to create an immutable class like the predefined String class.
We cannot make a class immutable without making it final.
● A final class is very useful when we want high security in any application because
the final class cannot be extended.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Key Points

● A constructor cannot be final.


● A block cannot be final.
● A local final variable must be initialized at the time of declaration.
● We cannot change the value of a final variable after initialization.
● We cannot override a final method.
● A final class cannot be extended(inherited).
● We can create the object for a final class but cannot extend it.
● If the method parameters are declared as final, the value of these parameters
cannot be changed.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Abstract

● Abstract is a non-access modifier in Java which is applicable for classes, interfaces,


methods, and inner classes.
● It represents an incomplete class that depends on subclasses for its implementation.
Creating subclass is compulsory for abstract class.
● A non-abstract class is sometimes called a concrete class.
● An abstract concept is not applicable to variables.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Features of Abstract class in Java

● Abstract class is not a pure abstraction in Java.


● In Java, object creation is not possible for an abstract class because it is a partially
implemented class, not fully implemented class.
● It can be abstract even with no abstract method.
● It can have one or more abstract methods or non-abstract methods (or concrete
methods) or a combination of both methods.
● Abstract class allows to define private, final, static and concrete methods

TNS India Foundation | ‹#›


Partners in Economic Transformation
Example

TNS India Foundation | ‹#›


Partners in Economic Transformation
Benefits of Abstract Keyword

● An abstract class can be used when we need to share the same method to all non-
abstract subclasses with their own specific implementations.
● The common member of the abstract class can also be shared by the subclasses.
Thus, abstract class is useful to make the program more flexible and understandable.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Real Time Example

● A car owner knows that the accelerator pedal is used to increase the speed of car,
and pressing the brake pedal stops it. To perform these simple actions, you only need
to know how to use these components but not need to know how they function.

● When you need to send SMS from your mobile, you only type the text and send the
message. But you don’t know the internal processing of the message delivery.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. What is the purpose of the abstract keyword in Java?

A
It is used to create abstract data types.
B
It is used to declare an abstract class or method.

C
It is used to create interfaces.
D
It is used to define constants.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. What does the final keyword signify in Java?

A
It is used to indicate that a variable cannot be changed
B
It is used to specify that a class cannot be extended.

C
It is used to define a constant.
D
It is used to create an instance of a class.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Summary

● Static is a keyword that is used for memory management mainly.


● The final keyword declared with variable, method, and class indicates that “This cannot
be modified”
● Abstract is a non-access modifier in Java which is applicable for classes, interfaces,
methods, and inner classes.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question
Banking Transaction System

Objective: Design and implement a Banking Transaction System using Java, incorporating
static, abstract, and final.

Instructions:

1.Static:

a. Implement a Bank class with a static variable totalAccounts to keep track of the total
number of bank accounts.

b. Create a static method getTotalAccounts() that returns the current value of totalAccounts.

c. Utilize the static keyword to ensure that totalAccounts is shared among all instances of the
TNS India Foundation | ‹#›
Bank class.
Partners in Economic Transformation
2.Abstract:

a. Create an abstract class Account with abstract methods like deposit(), withdraw(), and
getBalance().

b. Implement concrete methods for common functionality in the Account class.

c. Create two subclasses, SavingsAccount and CheckingAccount, inheriting from the


Account class.

d. Implement specific functionality for deposit, withdrawal, and balance retrieval in each
subclass.

TNS India Foundation | ‹#›


Partners in Economic Transformation
3.Final :

a. Design a Transaction class with final methods such as performTransaction() to ensure


that the transaction process is consistent across all instances.

b. Create a final variable transactionFee that represents a fixed fee for each transaction.

c. Demonstrate how the final methods and variables contribute to maintaining the integrity of
the transaction process.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Let’s Reflect -

1. What is your major learning from today’s session?


2. How are you going to use this learning in your journey on the job?

TNS India Foundation | ‹#›


Partners in Economic Transformation

You might also like