SlideShare a Scribd company logo
1 of 33
Download to read offline
Class in Java
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Agenda
• Introduction to a Class
• Requirement of a Class
• Elements of a Class
• Defining a Class
• Memory allocation
• Coding standards for Classes
• Access modifiers used for Classes
• Encapsulation
• Example programs for Class creation
• Inner Class
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Class
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Class
• User-defined data type
• Representation of an object-oriented approach
• Process of binding data members and methods in a single unit
• No implementation is possible without classes in java
• Example
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Requirement of a Class
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Requirement of Class
• Everything is treated as an object in an object-oriented programming language
• The class provides meaning to the objects
• Enhancements become easy
• Maintainability and Modularity will be improved
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Elements of a Class
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Elements of a class
Classes consist of the following:-
• Methods/Behaviors
They are methods that decides the action/task be performed by
the object
• Variables/Attributes
They are the data members that an object consists of.
• In real scenarios class is represented using a Class diagram created using UML concepts and
software
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Defining a class
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Defining a Class
Following is the syntax followed for defining a class in java:-
class <name_of_the_class>
{
//variable declaration
//methods declaration
}
It’s a keyword in java
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Memory Allocation
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Memory Allocation
• Java classes have logical existence
• Defining a class doesn’t allocate memory space.
• Memory space is created only when objects are created.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Coding standards for Classes
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Coding standards for Classes
Some specific coding standards are followed for defining a class in java
1. Starting letter should be in capitals. If the class name consists of more than one word then first
letter of each word should be in capitals (camel case is followed)
2. White spaces are not allowed in the class name
3. Letters, digits, dollar signs, and underscores can be used
4. Choosing a full name in place of cryptic abbreviations is a good programming practice
5. Avoid using java keywords as the name of the class
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Coding Concepts related to Classes
• We always run a java class
• For every class, one .class file is created
• The main method of a corresponding class is executed, if the main method is not present it
throws a Runtime error
• Highly recommended to take one class for the source file and the name of the source file and
name of the class should be the same
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Access modifiers used for Classes
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Access modifiers used for classes
The applicable modifiers for top level class are:-
• public
• <default>
• final
• abstract
• strictfp
Inner Class applicable modifiers are :-
• Private
• Protected
• Static
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Access modifiers used for classes
Public:-
If a class is declared public, it can be accessed from anywhere.
Default:-
It is a by default used access specifier in java. If a class is declared default then that class can be
accessed only within that package
Final:-
If a class is declared final, then that class cannot be inherited
Abstract:-
A class is declared as abstract then its instantiation is not possible.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Access modifiers used for classes
Private:-
Only an inner class can be declared private and a private inner class can be accessed only within the
outer class
Protected:-
If an inner class is declared as protected then it can be accessed only within the outer class and its
child class
Static:-
A class cannot be declared as static but for inner classes, it can be used
strictfp:-
A class is declared as strictfp then all its methods should follow IEEE754 standards
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Encapsulation
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Encapsulation
It is the method of encapsulating the functions and data members into a single unit.
Every java class and package is an encapsulated component.
Example:-
class bank
{
String bankName;
void printName()
{
System.out.println(“name of bank is State bank of india”);
}
}
Encapsulated
component
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Question
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Question
• Defining a class in java
 Let’s write a class definition for a student who has his name, roll no, and marks of three subjects.
 The class student should have two methods to calculate the total marks of a student and get the
grades
• Accessing the defined methods and variables
 Create an object of the class to call the methods and access the data members inside the main
method
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Inner Class
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Inner Class
• It’s a way to declare a class inside another class
• When there is no chance of a existence of an object without an existence of another object then we
use the inner class concept.
• The relationship between outer and inner class is a has-a relationship
Example:-
Without Bank there can’t be a Account. So,
class Bank
{
class Account
{
}
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Inner Class
• Inside inner class, static member should not be declared.
• How will .class file be created and executed for these inner classes?
class Bank
{
class Account
{
}
public static void main(String[] args)
{
System.out.println(“account created”);
}
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Inner Class
Accessing Inner class from the static area of outer class.
public class Bank
{
class Account
{
public void printAccountName()
{
System.out.println(“Bank account name is abcd”);
}
}
public static void main(String[] args)
{
Bank b = new Bank();
Bank.Account a = b.new Bank();
a.printAccountName();
} } Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Inner Class
Accessing Inner class from the instance area of outer class.
public class Bank
{
class Account
{
public void printAccountName()
{
System.out.println(“Bank account name is abcd”);
}
}
public void callingMethod()
{
Account a = new Account();
a.printAccountName();
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Continued…
public static void main(String[] args)
{
Bank b = new Bank();
b.callingMethod();
}
}
• Nesting of the inner class is also possible
Class A
{
class B
{
class C
{ //codes and functionalities
}
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Anonymous inner class
• For one-time usage of an inner class, we create an anonymous inner class.
• It’s an inner class without a name
• There are 3 types of anonymous inner class
• Anonymous inner class that extends a class
• Anonymous inner class that implements an interface
• Anonymous inner class that is defined inside arguments
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Anonymous inner class that extends a class
public class Bank
{
public void printAccountName()
{
System.out.println(“Bank account name is abcd”);
}
class Account
{
public static void main(String[] args)
{
Bank b1 = new Bank()
{
public void printAccountName()
{
System.out.println(“Bank account name is xyz”);
}
};
b1.printAccountName();
Bank b2 = new Bank();
b2.printAccountName();
}
}
Inner
class
definition
Calling the methods
defined inside inner
class
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Summary
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Summary
Discussed the following topics:-
1. What is a class and how to define a class in java
2. The different coding guidelines and standards to be followed for
classes in java
3. Different access modifiers suitable for classes at different levels
4. Class is the best example to implement encapsulation
5. The complete concept of inner classes and it’s implementations
6. Examples related to classes.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

More Related Content

Similar to Classes in Java great learning.pdf

Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingRatnaJava
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxDaveEstonilo
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritancemcollison
 
Chapter08 - Inheritance.ppt
Chapter08 - Inheritance.pptChapter08 - Inheritance.ppt
Chapter08 - Inheritance.pptPatrick Okot
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
Top 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersTop 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersWhizlabs
 
Inheritance in oop
Inheritance in oopInheritance in oop
Inheritance in oopMuskanNazeer
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingWondimuBantihun1
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingVasilios Kuznos
 
Topic inheritance
Topic  inheritanceTopic  inheritance
Topic inheritanceAnkit Kumar
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptxAsifMulani17
 
SystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfSystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfssusere9cd04
 

Similar to Classes in Java great learning.pdf (20)

Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Javasession8
Javasession8Javasession8
Javasession8
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
 
Java_notes.ppt
Java_notes.pptJava_notes.ppt
Java_notes.ppt
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
Chapter08 - Inheritance.ppt
Chapter08 - Inheritance.pptChapter08 - Inheritance.ppt
Chapter08 - Inheritance.ppt
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Top 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersTop 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed Answers
 
Inheritance in oop
Inheritance in oopInheritance in oop
Inheritance in oop
 
Inner Classes in Java
Inner Classes in JavaInner Classes in Java
Inner Classes in Java
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Topic inheritance
Topic  inheritanceTopic  inheritance
Topic inheritance
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptx
 
Lecture09.ppt
Lecture09.pptLecture09.ppt
Lecture09.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
SystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfSystemVerilog_Classes.pdf
SystemVerilog_Classes.pdf
 

Recently uploaded

(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 

Recently uploaded (20)

(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 

Classes in Java great learning.pdf

  • 1. Class in Java Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 2. Agenda • Introduction to a Class • Requirement of a Class • Elements of a Class • Defining a Class • Memory allocation • Coding standards for Classes • Access modifiers used for Classes • Encapsulation • Example programs for Class creation • Inner Class Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 3. Introduction to Class Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 4. Introduction to Class • User-defined data type • Representation of an object-oriented approach • Process of binding data members and methods in a single unit • No implementation is possible without classes in java • Example Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 5. Requirement of a Class Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 6. Requirement of Class • Everything is treated as an object in an object-oriented programming language • The class provides meaning to the objects • Enhancements become easy • Maintainability and Modularity will be improved Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 7. Elements of a Class Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 8. Elements of a class Classes consist of the following:- • Methods/Behaviors They are methods that decides the action/task be performed by the object • Variables/Attributes They are the data members that an object consists of. • In real scenarios class is represented using a Class diagram created using UML concepts and software Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 9. Defining a class Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 10. Defining a Class Following is the syntax followed for defining a class in java:- class <name_of_the_class> { //variable declaration //methods declaration } It’s a keyword in java Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 11. Memory Allocation Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 12. Memory Allocation • Java classes have logical existence • Defining a class doesn’t allocate memory space. • Memory space is created only when objects are created. Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 13. Coding standards for Classes Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 14. Coding standards for Classes Some specific coding standards are followed for defining a class in java 1. Starting letter should be in capitals. If the class name consists of more than one word then first letter of each word should be in capitals (camel case is followed) 2. White spaces are not allowed in the class name 3. Letters, digits, dollar signs, and underscores can be used 4. Choosing a full name in place of cryptic abbreviations is a good programming practice 5. Avoid using java keywords as the name of the class Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 15. Coding Concepts related to Classes • We always run a java class • For every class, one .class file is created • The main method of a corresponding class is executed, if the main method is not present it throws a Runtime error • Highly recommended to take one class for the source file and the name of the source file and name of the class should be the same Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 16. Access modifiers used for Classes Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 17. Access modifiers used for classes The applicable modifiers for top level class are:- • public • <default> • final • abstract • strictfp Inner Class applicable modifiers are :- • Private • Protected • Static Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 18. Access modifiers used for classes Public:- If a class is declared public, it can be accessed from anywhere. Default:- It is a by default used access specifier in java. If a class is declared default then that class can be accessed only within that package Final:- If a class is declared final, then that class cannot be inherited Abstract:- A class is declared as abstract then its instantiation is not possible. Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 19. Access modifiers used for classes Private:- Only an inner class can be declared private and a private inner class can be accessed only within the outer class Protected:- If an inner class is declared as protected then it can be accessed only within the outer class and its child class Static:- A class cannot be declared as static but for inner classes, it can be used strictfp:- A class is declared as strictfp then all its methods should follow IEEE754 standards Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 20. Encapsulation Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 21. Encapsulation It is the method of encapsulating the functions and data members into a single unit. Every java class and package is an encapsulated component. Example:- class bank { String bankName; void printName() { System.out.println(“name of bank is State bank of india”); } } Encapsulated component Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 22. Question Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 23. Question • Defining a class in java  Let’s write a class definition for a student who has his name, roll no, and marks of three subjects.  The class student should have two methods to calculate the total marks of a student and get the grades • Accessing the defined methods and variables  Create an object of the class to call the methods and access the data members inside the main method Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 24. Inner Class Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 25. Inner Class • It’s a way to declare a class inside another class • When there is no chance of a existence of an object without an existence of another object then we use the inner class concept. • The relationship between outer and inner class is a has-a relationship Example:- Without Bank there can’t be a Account. So, class Bank { class Account { } } Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 26. Inner Class • Inside inner class, static member should not be declared. • How will .class file be created and executed for these inner classes? class Bank { class Account { } public static void main(String[] args) { System.out.println(“account created”); } } Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 27. Inner Class Accessing Inner class from the static area of outer class. public class Bank { class Account { public void printAccountName() { System.out.println(“Bank account name is abcd”); } } public static void main(String[] args) { Bank b = new Bank(); Bank.Account a = b.new Bank(); a.printAccountName(); } } Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 28. Inner Class Accessing Inner class from the instance area of outer class. public class Bank { class Account { public void printAccountName() { System.out.println(“Bank account name is abcd”); } } public void callingMethod() { Account a = new Account(); a.printAccountName(); } Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 29. Continued… public static void main(String[] args) { Bank b = new Bank(); b.callingMethod(); } } • Nesting of the inner class is also possible Class A { class B { class C { //codes and functionalities } } Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 30. Anonymous inner class • For one-time usage of an inner class, we create an anonymous inner class. • It’s an inner class without a name • There are 3 types of anonymous inner class • Anonymous inner class that extends a class • Anonymous inner class that implements an interface • Anonymous inner class that is defined inside arguments Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 31. Anonymous inner class that extends a class public class Bank { public void printAccountName() { System.out.println(“Bank account name is abcd”); } class Account { public static void main(String[] args) { Bank b1 = new Bank() { public void printAccountName() { System.out.println(“Bank account name is xyz”); } }; b1.printAccountName(); Bank b2 = new Bank(); b2.printAccountName(); } } Inner class definition Calling the methods defined inside inner class Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 32. Summary Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 33. Summary Discussed the following topics:- 1. What is a class and how to define a class in java 2. The different coding guidelines and standards to be followed for classes in java 3. Different access modifiers suitable for classes at different levels 4. Class is the best example to implement encapsulation 5. The complete concept of inner classes and it’s implementations 6. Examples related to classes. Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited