SlideShare a Scribd company logo
1 of 24
Download to read offline
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

Csharp4 inheritance
Csharp4 inheritanceCsharp4 inheritance
Csharp4 inheritance
Abed Bukhari
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
frwebhelp
 
03 Object Relational Mapping
03 Object Relational Mapping03 Object Relational Mapping
03 Object Relational Mapping
Ranjan Kumar
 

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

Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Csharp4 inheritance
Csharp4 inheritanceCsharp4 inheritance
Csharp4 inheritance
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Ej Chpt#4 Final
Ej Chpt#4 FinalEj Chpt#4 Final
Ej Chpt#4 Final
 
Custom gutenberg block development in react
Custom gutenberg block development in reactCustom gutenberg block development in react
Custom gutenberg block development in react
 
OOP and C++Classes
OOP and C++ClassesOOP and C++Classes
OOP and C++Classes
 
C++ L10-Inheritance
C++ L10-InheritanceC++ L10-Inheritance
C++ L10-Inheritance
 
03 Object Relational Mapping
03 Object Relational Mapping03 Object Relational Mapping
03 Object Relational Mapping
 
Java
JavaJava
Java
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 
Total Compensationbuild.xml Builds, tests, and runs th.docx
Total Compensationbuild.xml      Builds, tests, and runs th.docxTotal Compensationbuild.xml      Builds, tests, and runs th.docx
Total Compensationbuild.xml Builds, tests, and runs th.docx
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
 
Java
JavaJava
Java
 
Design patterns in Java - Monitis 2017
Design patterns in Java - Monitis 2017Design patterns in Java - Monitis 2017
Design patterns in Java - Monitis 2017
 
Patterns in PHP
Patterns in PHPPatterns in PHP
Patterns in PHP
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptx
 
So S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better CodeSo S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better Code
 
Java 17
Java 17Java 17
Java 17
 
The cost of learning - advantage of mixer2
The cost of learning - advantage of mixer2The cost of learning - advantage of mixer2
The cost of learning - advantage of mixer2
 

More from WebStackAcademy

More from WebStackAcademy (20)

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

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

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: info@www.webstackacademy.com www.webstackacademy.com