OOPJ-2 .1E-Learning PowerPoint Templates
OOPJ-2 .1E-Learning PowerPoint Templates
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
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
}
}
}
THANK YOU