0% found this document useful (0 votes)
5 views

Classes in Java

Uploaded by

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

Classes in Java

Uploaded by

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

Classes in Java

Introduction
 In Java, the class is a blueprint from which we
can create an individual object.
 Java provides a keyword named class by which

we can declare a class.


 Inside the class, we define class members and

functions. It is not possible to create Java


programs without class.
 We can also refer a class as a user-

defined data type because an object-oriented


paradigm allows us to model real-world
objects.
Types of Classes

 There are seven types of classes in Java:


◦ Static Class
◦ Final Class
◦ Abstract Class
◦ Concrete Class
◦ Singleton Class
◦ POJO Class
◦ Inner Class
a) Static Class
 In Java, static is a keyword that manage objects in the
memory.
 The static object belongs to the class instead of the
instance of the class.
 We can make a class static if and only if it is a nested
class. We can also say that static classes are known
as nested classes.
 It means that a class that is declared as static within
another class is known as a static class.
 Nested static class does not require reference to the
outer class. The purpose of a static class is to
provide the outline of its inherited class.
 The properties of the static class are:
◦ The class has only static members.
◦ It cannot access the member (non-static) of the
outer class.
◦ We cannot create an object of the static class.
Example
public class A
{
private static String str = “James";

public static class A1


{ public void show()
{
System.out.println(str);
}
}
public static void main(String args[])
{
A.A1 obj = new A.A1();
obj.show();
}
}
b) Final Class
 The word final means that cannot be changed.
The final class in Java can be declared using
the final keyword.
 Once we declare a class as final, the values

remain the same throughout the program.


 The purpose of the final class is to make the

class immutable like the String class.


 It is only a way to make the class immutable.

Remember that the final class cannot be


extended. It also prevents the class from being
sub-classed.
Example
final class AA
{
void d()
{
System.out.println("hi");
}
}
public class Q
{
public static void main(String s[])
{
AA a1=new AA();
a1.d();
}
}
c) Abstract Class
 An abstract class is a that is declared with the
keyword abstract.
 The class may or may not contain abstract methods.
We cannot create an instance of an abstract class but it
can be a subclass.
 These classes are incomplete, so to complete the
abstract class we should extend the abstract classes to
a concrete class.
 When we declare a subclass as abstract then it is
necessary to provide the implementation of abstract
methods.
 Therefore, the subclass must also be declared abstract.
We can achieve data hiding by using the abstract class.
Example
abstract class M
{
int a=30, b=40;
public abstract void add();
}
public class Operation extends M
{
public void add()
{
System.out.println("Sum of a and b is: "a+b);
}
public static void main(String args[])
{
M obj = new Operation();
obj.add();
}
}
d) Concrete Class

 These are the regular Java classes.


 A derived class that provides the basic
implementations for all of the methods that are not
already implemented in the base class is known as
a concrete class.
 In other words, it is regular Java classes in which all
the methods of an abstract class are implemented.
 We can create an object of the concrete class
directly. Remember that concrete class and
abstract class are not the same.
 A concrete class may extend its parent class. It is
used for specific requirements.
Example
//Concrete Class
public class ConcreteClassExample
{
//method of the concreted class
static int product(int a, int b)
{
return a * b;
}
public static void main(String args[])
{
//method calling
int p = product(6, 8);
System.out.println("Product of a and b is: " + p);
}
}
e) Singleton Class

 A class that has only an object at a time is known as


a singleton class. Still, if we are trying to create an
instance a second time, that newly created instance
points to the first instance.
 If we made any alteration inside the class through any
instance, the modification affects the variable of the
single instance, also. It is usually used to control
access while dealing with the database connection and
socket programming. If we want to create a singleton
class, do the following:
◦ Create a private constructor.
◦ Create a static method (by using the lazy initialization) that
returns the object of the singleton class.
Example
public class Singleton
{
private String objectState;
private static Singleton instance = null;
private Singleton() throws Exception
{ this.objectState = "James";
}
public static Singleton getInstance()
{ if(instance==null)
{ try {
instance=new Singleton();
}
catch(Exception e) { e.printStackTrace(); } }
return instance;
}
public String getObjectState()
{ return objectState; }
public void setObjectState(String objectState)
{ this.objectState = objectState; } }
f) POJO Class
(Plain Old Java Object)
 In Java, POJO stands for Plain Old Java Object. A Java
class that contains only private variables, setter and
getter is known as POJO class. It is used to define Java
objects that increase the reusability and readability of a
Java program. The class provides encapsulation. It is
widely used in Java because it is easy to understand
these classes. POJO class has the following properties:
◦ It does not extend the predefined classes such as Arrays,
HttpServlet, etc.
◦ It cannot contain pre-specified annotations.
◦ It cannot implement pre-defined interfaces.
◦ It is not required to add any constructor.
◦ All instance variables must be private.
◦ The getter/ setter methods must be public.
Example
Class A
{
private double price=123.34;
//getter method
public double getPrice()
{
return price;
}
//setter method
public void setPrice(int price)
{
this.price = price;
}
}
//main class
public class A1
{
public static void main(String args[])
{
A obj=new A();
System.out.println("The price of an article is "+ obj.getPrice()+" Rs."); } }
g) Inner class

 Java allows us to define a class within a class


and such classes are known as nested
classes.
 It is used to group the classes logically and to

achieve encapsulation.
 The outer class members (including private)

can be accessed by the inner class.


Example
public class A
{
public static void main(String[] args)
{
System.out.println("This is outer class.");
}
class InnerClass
{
public void printinner()
{
System.out.println("This is inner class.");
}
}
}

You might also like