More Related Content
Similar to Classes in Java great learning.pdf
Similar to Classes in Java great learning.pdf (20)
Classes in Java great learning.pdf
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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