SlideShare a Scribd company logo
www.webstackacademy.com
Java Programming Language SE – 6
Module 7 : Advanced Class Features
www.webstackacademy.com
Objectives
● Create static variables, methods, and initializers
● Create final classes, methods, and variables
● Create and use enumerated types
● Use the static import statement
● Create abstract classes and methods
● Create and use an interface
www.webstackacademy.com
Relevance
● How can you create a constant?
● How can you declare data that is shared by all instances of a given
class?
● How can you keep a class or method from being subclassed or
overridden?
www.webstackacademy.com
The static Keyword
● The static keyword is used as a modifier on variables, methods, and
nested classes.
● The static keyword declares the attribute or method is associated with
the class as a whole rather than any particular instance of that class.
● Thus static members are often called class members, such as class
attributes or class methods.
www.webstackacademy.com
Class Attributes/
Static Variables
● Class attributes are shared among all instances of a class.
● it can be accessed without an instance.
www.webstackacademy.com
Class Methods/
Static Methods
public static int getTotalCount() {
return counter;
}
● You can invoke static methods without any instance of the class to
which it belongs.
● Static methods cannot access instance variables.
www.webstackacademy.com
Static Initializers
● A class can contain code in a static block that does not exist within a
method body.
● Static block code executes once only, when the class is loaded.
● Usually, a static block is used to initialize static (class) attributes.
www.webstackacademy.com
Static Initializers: Example
static {
counter = Integer.getInteger("myApp.Count4.counter").intValue();
}
www.webstackacademy.com
The final Keyword
● You cannot subclass a final class.
● You cannot override a final method.
● A final variable is a constant.
● You can set a final variable once only, but that assignment can occur
independently of the declaration; this is called a blank final variable.
● A blank final instance attribute must be set in every constructor.
● A blank final method variable must be set in the method body before
being used.
www.webstackacademy.com
Final Variables
Constants are static final variables.
public class Bank {
private static final double
... // more declarations
}
www.webstackacademy.com
Blank Final Variables
private final long customerID;
public Customer() {
customerID = createID();
}
www.webstackacademy.com
Enumerated Type
package cards.domain;
public enum Suit {
SPADES,
HEARTS,
CLUBS,
DIAMONDS
}
www.webstackacademy.com
Enumerated Type: Example
package cards.domain;
public class PlayingCard {
private Suit suit;
private int rank;
public PlayingCard(Suit suit, int rank) {
this.suit = suit;
this.rank = rank;
}
public Suit getSuit() {
return suit;
}
www.webstackacademy.com
Enumerated Type
public String getSuitName() {
String name = ““;
switch ( suit ) {
case SPADES:
name = “Spades”;
break;
case HEARTS:
name = “Hearts”;
break;
case CLUBS:
name = “Clubs”;
break;
case DIAMONDS:
name = “Diamonds”;
break;
default:
}return name;}
www.webstackacademy.com
Enumerated Type
● Enumerated types are type-safe:
package cards.tests;
import cards.domain.PlayingCard;
import cards.domain.Suit;
public class TestPlayingCard {
public static void main(String[] args) {
PlayingCard card1 = new PlayingCard(Suit.SPADES, 2);
System.out.println(“card1 is the “ + card1.getRank() + “ of “ +
card1.getSuitName());
// PlayingCard card2 = new PlayingCard(47, 2);
// This will not compile.
}}
www.webstackacademy.com
Advanced Enumerated
Types
● Enumerated types can have attributes and methods:
package cards.domain;
public enum Suit {
SPADES
(“Spades”),
HEARTS
(“Hearts”),
CLUBS
(“Clubs”),
DIAMONDS (“Diamonds”);
private final String name;
private Suit(String name) {
this.name = name;
}
public String getName() {
return name;}}
www.webstackacademy.com
Advanced Enumerated
Types
package cards.tests;
import cards.domain.PlayingCard;
import cards.domain.Suit;
public class TestPlayingCard {
public static void main(String[] args) {
PlayingCard card1 = new PlayingCard(Suit.SPADES, 2);
System.out.println(“card1 is the “ + card1.getRank()
+ “ of “ + card1.getSuit().getName());
// NewPlayingCard card2 = new NewPlayingCard(47, 2);
// This will not compile.
}}
www.webstackacademy.com
Static Imports
● A static import imports the static members from a class:
import static <pkg_list>.<class_name>.<member_name>;
OR
import static <pkg_list>.<class_name>.*;
● A static import imports members individually or collectively:
import static cards.domain.Suit.SPADES;
OR
import static cards.domain.Suit.*;
www.webstackacademy.com
Abstract Classes
public class FuelNeedsReport {
private Company company;
public FuelNeedsReport(Company company) {
this.company = company;
}
public void generateText(PrintStream output) {
Vehicle1 v;
double fuel;
double total_fuel = 0.0;
for ( int i = 0; i < company.getFleetSize(); i++ ) {
v = company.getVehicle(i);
www.webstackacademy.com
Abstract Classes
// Calculate the fuel needed for this trip
fuel = v.calcTripDistance() / v.calcFuelEfficency();
output.println("Vehicle " + v.getName() + " needs "
+ fuel + " liters of fuel.");
total_fuel += fuel;
}
output.println("Total fuel needs is " + total_fuel + " liters.");
}}
www.webstackacademy.com
Abstract Classes
● An abstract class models a class of objects in which the full
implementation is not known but is supplied by the concrete
subclasses.
www.webstackacademy.com
Interfaces
● A public interface is a contract between client code and the class that
implements that interface.
● A Java interface is a formal declaration of such a contract in which all
methods contain no implementation.
● Many unrelated classes can implement the same interface.
● A class can implement many unrelated interfaces.
● Syntax of a Java class is as follows:
<modifier> class <name> [extends <superclass>]
[implements <interface> [,<interface>]* ] {
<member_declaration>*
}
www.webstackacademy.com
Uses of Interfaces
Interface uses include the following:
● Declaring methods that one or more classes are expected to
implement
● Determining an object’s programming interface without revealing the
actual body of the class
● Capturing similarities between unrelated classes without forcing a
class relationship
● Simulating multiple inheritance by declaring a class that implements
several interfaces
Web Stack Academy (P) Ltd
#83, Farah Towers,
1st floor,MG Road,
Bangalore – 560001
M: +91-80-4128 9576
T: +91-98862 69112
E: info@www.webstackacademy.com
www.webstackacademy.com

More Related Content

Similar to Core Java Programming Language (JSE) : Chapter VII - Advanced Class Features (20)

PPTX
3.Classes&Objects.pptx
PRABHUSOLOMON1
 
PPTX
Ch-2ppt.pptx
ssuser8347a1
 
PPTX
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
PPTX
Android Training (Java Review)
Khaled Anaqwa
 
PPT
Java Classes methods and inheritance
Srinivas Reddy
 
PPTX
BCA Class and Object (3).pptx
SarthakSrivastava70
 
PPTX
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
PPTX
OOPJ_PPT1, Inheritance, Encapsulation Polymorphism Static Variables.pptx
SrinivasGopalan2
 
PDF
03_Objects and Classes in java.pdf
Parameshwar Maddela
 
PPTX
Class and Object.pptx from nit patna ece department
om2348023vats
 
PPTX
Lecture 6.pptx
AshutoshTrivedi30
 
PDF
Java/J2EE interview Qestions
Arun Vasanth
 
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
PPTX
UNIT - IIInew.pptx
akila m
 
DOCX
javaopps concepts
Nikhil Agrawal
 
PPT
Java-Variables_about_different_Scope.ppt
JyothiAmpally
 
PPT
Object and class
mohit tripathi
 
PDF
this keyword in Java.pdf
ParvizMirzayev2
 
PPTX
Module 4_Ch2 - Introduction to Object Oriented Programming.pptx
jhesorleylaid2
 
PPTX
Class as the Basis of all Computation.pptx
kinjalahuja087
 
3.Classes&Objects.pptx
PRABHUSOLOMON1
 
Ch-2ppt.pptx
ssuser8347a1
 
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
Android Training (Java Review)
Khaled Anaqwa
 
Java Classes methods and inheritance
Srinivas Reddy
 
BCA Class and Object (3).pptx
SarthakSrivastava70
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
OOPJ_PPT1, Inheritance, Encapsulation Polymorphism Static Variables.pptx
SrinivasGopalan2
 
03_Objects and Classes in java.pdf
Parameshwar Maddela
 
Class and Object.pptx from nit patna ece department
om2348023vats
 
Lecture 6.pptx
AshutoshTrivedi30
 
Java/J2EE interview Qestions
Arun Vasanth
 
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
UNIT - IIInew.pptx
akila m
 
javaopps concepts
Nikhil Agrawal
 
Java-Variables_about_different_Scope.ppt
JyothiAmpally
 
Object and class
mohit tripathi
 
this keyword in Java.pdf
ParvizMirzayev2
 
Module 4_Ch2 - Introduction to Object Oriented Programming.pptx
jhesorleylaid2
 
Class as the Basis of all Computation.pptx
kinjalahuja087
 

More from WebStackAcademy (20)

PDF
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
PDF
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
PDF
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
PDF
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
PDF
Webstack Academy - Internship Kick Off
WebStackAcademy
 
PDF
Building Your Online Portfolio
WebStackAcademy
 
PDF
Front-End Developer's Career Roadmap
WebStackAcademy
 
PDF
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PDF
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
PDF
Angular - Chapter 5 - Directives
WebStackAcademy
 
PDF
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
PDF
Angular - Chapter 3 - Components
WebStackAcademy
 
PDF
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
 
PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
PDF
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
PDF
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
Webstack Academy - Internship Kick Off
WebStackAcademy
 
Building Your Online Portfolio
WebStackAcademy
 
Front-End Developer's Career Roadmap
WebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
Angular - Chapter 5 - Directives
WebStackAcademy
 
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Angular - Chapter 3 - Components
WebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
Angular - Chapter 1 - Introduction
WebStackAcademy
 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Ad

Recently uploaded (20)

PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Ad

Core Java Programming Language (JSE) : Chapter VII - Advanced Class Features

  • 1. www.webstackacademy.com Java Programming Language SE – 6 Module 7 : Advanced Class Features
  • 2. www.webstackacademy.com Objectives ● Create static variables, methods, and initializers ● Create final classes, methods, and variables ● Create and use enumerated types ● Use the static import statement ● Create abstract classes and methods ● Create and use an interface
  • 3. www.webstackacademy.com Relevance ● How can you create a constant? ● How can you declare data that is shared by all instances of a given class? ● How can you keep a class or method from being subclassed or overridden?
  • 4. www.webstackacademy.com The static Keyword ● The static keyword is used as a modifier on variables, methods, and nested classes. ● The static keyword declares the attribute or method is associated with the class as a whole rather than any particular instance of that class. ● Thus static members are often called class members, such as class attributes or class methods.
  • 5. www.webstackacademy.com Class Attributes/ Static Variables ● Class attributes are shared among all instances of a class. ● it can be accessed without an instance.
  • 6. www.webstackacademy.com Class Methods/ Static Methods public static int getTotalCount() { return counter; } ● You can invoke static methods without any instance of the class to which it belongs. ● Static methods cannot access instance variables.
  • 7. www.webstackacademy.com Static Initializers ● A class can contain code in a static block that does not exist within a method body. ● Static block code executes once only, when the class is loaded. ● Usually, a static block is used to initialize static (class) attributes.
  • 8. www.webstackacademy.com Static Initializers: Example static { counter = Integer.getInteger("myApp.Count4.counter").intValue(); }
  • 9. www.webstackacademy.com The final Keyword ● You cannot subclass a final class. ● You cannot override a final method. ● A final variable is a constant. ● You can set a final variable once only, but that assignment can occur independently of the declaration; this is called a blank final variable. ● A blank final instance attribute must be set in every constructor. ● A blank final method variable must be set in the method body before being used.
  • 10. www.webstackacademy.com Final Variables Constants are static final variables. public class Bank { private static final double ... // more declarations }
  • 11. www.webstackacademy.com Blank Final Variables private final long customerID; public Customer() { customerID = createID(); }
  • 12. www.webstackacademy.com Enumerated Type package cards.domain; public enum Suit { SPADES, HEARTS, CLUBS, DIAMONDS }
  • 13. www.webstackacademy.com Enumerated Type: Example package cards.domain; public class PlayingCard { private Suit suit; private int rank; public PlayingCard(Suit suit, int rank) { this.suit = suit; this.rank = rank; } public Suit getSuit() { return suit; }
  • 14. www.webstackacademy.com Enumerated Type public String getSuitName() { String name = ““; switch ( suit ) { case SPADES: name = “Spades”; break; case HEARTS: name = “Hearts”; break; case CLUBS: name = “Clubs”; break; case DIAMONDS: name = “Diamonds”; break; default: }return name;}
  • 15. www.webstackacademy.com Enumerated Type ● Enumerated types are type-safe: package cards.tests; import cards.domain.PlayingCard; import cards.domain.Suit; public class TestPlayingCard { public static void main(String[] args) { PlayingCard card1 = new PlayingCard(Suit.SPADES, 2); System.out.println(“card1 is the “ + card1.getRank() + “ of “ + card1.getSuitName()); // PlayingCard card2 = new PlayingCard(47, 2); // This will not compile. }}
  • 16. www.webstackacademy.com Advanced Enumerated Types ● Enumerated types can have attributes and methods: package cards.domain; public enum Suit { SPADES (“Spades”), HEARTS (“Hearts”), CLUBS (“Clubs”), DIAMONDS (“Diamonds”); private final String name; private Suit(String name) { this.name = name; } public String getName() { return name;}}
  • 17. www.webstackacademy.com Advanced Enumerated Types package cards.tests; import cards.domain.PlayingCard; import cards.domain.Suit; public class TestPlayingCard { public static void main(String[] args) { PlayingCard card1 = new PlayingCard(Suit.SPADES, 2); System.out.println(“card1 is the “ + card1.getRank() + “ of “ + card1.getSuit().getName()); // NewPlayingCard card2 = new NewPlayingCard(47, 2); // This will not compile. }}
  • 18. www.webstackacademy.com Static Imports ● A static import imports the static members from a class: import static <pkg_list>.<class_name>.<member_name>; OR import static <pkg_list>.<class_name>.*; ● A static import imports members individually or collectively: import static cards.domain.Suit.SPADES; OR import static cards.domain.Suit.*;
  • 19. www.webstackacademy.com Abstract Classes public class FuelNeedsReport { private Company company; public FuelNeedsReport(Company company) { this.company = company; } public void generateText(PrintStream output) { Vehicle1 v; double fuel; double total_fuel = 0.0; for ( int i = 0; i < company.getFleetSize(); i++ ) { v = company.getVehicle(i);
  • 20. www.webstackacademy.com Abstract Classes // Calculate the fuel needed for this trip fuel = v.calcTripDistance() / v.calcFuelEfficency(); output.println("Vehicle " + v.getName() + " needs " + fuel + " liters of fuel."); total_fuel += fuel; } output.println("Total fuel needs is " + total_fuel + " liters."); }}
  • 21. www.webstackacademy.com Abstract Classes ● An abstract class models a class of objects in which the full implementation is not known but is supplied by the concrete subclasses.
  • 22. www.webstackacademy.com Interfaces ● A public interface is a contract between client code and the class that implements that interface. ● A Java interface is a formal declaration of such a contract in which all methods contain no implementation. ● Many unrelated classes can implement the same interface. ● A class can implement many unrelated interfaces. ● Syntax of a Java class is as follows: <modifier> class <name> [extends <superclass>] [implements <interface> [,<interface>]* ] { <member_declaration>* }
  • 23. www.webstackacademy.com Uses of Interfaces Interface uses include the following: ● Declaring methods that one or more classes are expected to implement ● Determining an object’s programming interface without revealing the actual body of the class ● Capturing similarities between unrelated classes without forcing a class relationship ● Simulating multiple inheritance by declaring a class that implements several interfaces
  • 24. Web Stack Academy (P) Ltd #83, Farah Towers, 1st floor,MG Road, Bangalore – 560001 M: +91-80-4128 9576 T: +91-98862 69112 E: [email protected] www.webstackacademy.com