0% found this document useful (0 votes)
9 views10 pages

OOPJ-2 .1E-Learning PowerPoint Templates

Uploaded by

Heer Patel
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)
9 views10 pages

OOPJ-2 .1E-Learning PowerPoint Templates

Uploaded by

Heer Patel
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/ 10

Object Oriented

Programming with
Java(CT405-N)
Prof. Vimal Bhatt , Assistant
Professor
Computer Science & Engineering
Vidush Somany Institute of Technology & Research,Kadi
Chapter 2
Basics of objects and
classes in java
Inbuilt classes

What is class Static Class


 In Java, the class is a blueprint from which we can create an  In Java, static is a keyword that manage objects in the memory. The
individual object. Java provides a keyword named class by which
we can declare a class. static object belongs to the class
. instead of the instance of the class.
 Inside the class, we define class members and functions. It is not  We can make a class static if and only if it is a nested class.
possible to create Java programs without class.  We can also say that static classes are known as nested classes. It
 We can also refer a class as a user-defined data type because an means that a class that is declared as static within another class is
object-oriented paradigm allows us to model real-world objects. In known as a static class.
this section, we will focus on the types of classes in Java.  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.
Types of Inbuilt classes

Static Class The properties of the static class are:


Final Class
Abstract Class  The class has only static members.
Concrete Class  It cannot access the member (non-static) of the outer class.
Singleton Class  We cannot create an object of the static class.
POJO Class  Let's understand the concept of static class through a program.
Inner Class
Classes

Final Class
Example  The word final means that cannot be changed. The final class in
Java can be declared using the final keyword. Once we declare a
public class StaticClassExample 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
private static String str = "Javatpoint"; the class immutable. Remember that the final class cannot be
//nested class which is a Static class extended. It also prevents the class from being sub-classed.
public static class StaticDemo /base class declared as final
{ final class A
{
//non-static method of Static class
void printmsg()
public void show() {
{ System.out.print("Base class method is executed.");
}
System.out.println(str);
}
} //derived class
} //extending a final class which is not possible
//it shows the error cannot inherit final class at compile time
public static void main(String args[])
class B extends A
{ {
StaticClassExample.StaticDemo obj = new StaticClassExample.StaticDemo(); void printmsg()
{
System.out.print("Derived class method is executed.");
obj.show(); }
} }
//main class
}
public class FinalClassExample
{
public static void main(String[] arg)
{
B obj = new B();
obj.printmsg();
Classes

Abstract Class
 An abstract class is a that is declared with the keyword abstract. //abstract class
The class may or may not contain abstract methods. abstract class MathematicalOperations
 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. int a=30, b=40;
 When we declare a subclass as abstract then it is necessary to //abstract method
provide the implementation of abstract methods. Therefore, the public abstract void add();
subclass must also be declared abstract. }
 We can achieve data hiding by using the abstract class. An example public class Operation extends MathematicalOperations
of an abstract class is AbstarctMap class that is a part of the
{
Collections framework.
//definition of abstract method
public void add()
{
System.out.println("Sum of a and b is: "a+b);
}
public static void main(String args[])
{
MathematicalOperations obj = new Operation();
obj.add();
}
}
Concrete Class
 These are the regular Java classes. A derived class that provides the 1.//Concrete Class
basic implementations for all of the methods that are not already public class ConcreteClassExample
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 //method of the concreted class
abstract class are implemented. static int product(int a, int b)
 We can create an object of the concrete class directly. Remember that { .
concrete class and abstract class are not the same. A concrete class return a * b;
may extend its parent class. It is used for specific requirements. }
public static void main(String args[])
{
//method calling
int p = product(6, 8);
System.out.println("Product of a and b is: " + p);
}
}
Singleton Class
public class Singleton
 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 private String objectState;
newly created instance points to the first instance. private static Singleton instance = null;
 If we made any alteration inside the class through any instance, the private Singleton() throws Exception
modification affects the variable of the single instance, also. It is {
this.objectState
. = "Javatpoint";
usually used to control access while dealing with the database
}
connection and socket programming. If we want to create a singleton public static Singleton getInstance()
class, do the following: {
 Create a private constructor. if(instance==null)
 Create a static method (by using the lazy initialization) that returns the {
object of the singleton class. try
{
instance=new Singleton();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return instance;
}
public String getObjectState()
{
return objectState;
}
public void setObjectState(String objectState)
{
this.objectState = objectState;
}
}
Static Variable in Java
 Static variables defined as a class member can be accessed without public class Test
object of that class. Static variable is initialized once and shared {
among different objects of the class. All the object of the class having static int x = 100;
static variable will have the same instance of static variable.
 Note: Static variable is used to represent common property of a class.
int y = 100;
It saves memory. public void increment()
 Example: {
 Suppose there are 100 employee in a company. All employee have its x++; y++;
unique name and employee id but company name will be same all 100
employee. Here company name is the common property. So if you }
create a class to store employee detail, then declare company_name public static void main( String[] args )
field as static {
 Below we have a simple class with one static variable, see the
example.
Test t1 = new Test();
Test t2 = new Test();
t1.increment();
t2.increment();
System.out.println(t2.y);
System.out.println(Test.x); //accessed without any
instance of class.
}
class ST_Employee
{
int eid; class Test
String name; {
static String company_name;
public static void square(int x)
static { {
company_name ="StudyTonight"; //static block invoked System.out.println(x*x);
before main() method
}
}

public void show() public static void main (String[] arg)


{ {
System.out.println(eid+" "+name+" "+company_name); square(8) //static method square () is called withou
} any instance of class.
public static void main( String[] args ) }
{ }
ST_Employee se1 = new ST_Employee();
se1.eid = 104;
se1.name = "Abhijit";
se1.show();
}

}
THANK YOU

You might also like