0% found this document useful (0 votes)
49 views46 pages

Lec 3-8

object oriented programming class lecture

Uploaded by

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

Lec 3-8

object oriented programming class lecture

Uploaded by

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

CS F213 Object Oriented Programming

Object Model: “Classes and Objects”

Aritra Mukherjee, Dept. of CSIS


The four pillars of OOP
Some examples of class and objects
Some examples of class and objects
Some examples of class and objects
Some examples of class and objects
Object and Class, the relations
Making an object from a class
Ways to Create an Object of a Class
1. Using new keyword

It is the most common and general way to create an object in Java.


Ways to Create an Object of a Class
2. Using Class.forName(String className) method

There is a pre-defined class in java.lang package with name Class. The


forName(String className) method returns the Class object associated with the
class with the given string name. We have to give a fully qualified name for a class.
On calling the new Instance() method on this Class object returns a new instance of
the class with the given string name.
Ways to Create an Object of a Class
3. Using clone() method

clone() method is present in the Object class. It creates and returns a copy of the
object.
Ways to Create an Object of a Class
4. Deserialization

De-serialization is a technique of reading an object from the saved state in a file.


Anonymous Objects in Java
● They are used for immediate method calls.
● They will be destroyed after method calling.
Types of classes in java
Static Class
● 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.

● 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.
Static Class
● If we declare that variable as
non-static, the compiler shows an
error because a nested static
class cannot access non-static
members of the outer class.
● It is to be noticed that for creating
the object of the nested class we
need not to create an instance of
the outer class. If the nested
class is not a static class, we
need to create an instance of the
outer class.
Final Class
● 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.
Final Class
Final Class
Final Class
Abstract Class
More in Interface Later
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.
● We can achieve data hiding by
using the abstract class.
Abstract Class
● An abstract class must have at least one abstract method.
● An abstract class includes final methods.
● Abstraction in Java is expensive, as sometimes you need to handle cases
and situations which are not always necessary
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.
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.
Singleton Class
Singleton Class
POJO class
● It does not extend the
● In Java, POJO stands for predefined classes such as
Plain Old Java Object. Arrays, HttpServlet, etc.
● A Java class that contains ● It cannot contain
only private variables, setter pre-specified annotations.
and getter is known as POJO ● It cannot implement
class. predefined interfaces.
● It is used to define Java ● It is not required to add any
objects that increase the constructor.
reusability and readability of a
● All instance variables must be
Java program. The class
provides encapsulation. private.
● The getter/ setter methods
must be public.
POJO class
POJO class
Inner class ● Static Nested class: A class that is
static and nested is called a static
● Java allows us to define a
nested class. It interacts with the
class within a class and such
instance member of its outer class.
classes are known as nested
We can create an object of the static
classes.
nested class by using the following
● It is used to group the classes
syntax:
logically and to achieve
encapsulation. The outer OuterClass.StaticNestedClass
class members (including nestedObject = new
private) can be accessed by OuterClass.StaticNestedClass();
the inner class.
Inner class ● Non-static Nested Class: Non-static nested
classes are called inner classes. The
general syntax for declaring the static
● Java allows us to define a
nested class and inner class is as follows:
class within a class and such
class OuterClass {
classes are known as nested
static class StaticNestedClass {
classes.
● It is used to group the classes //something
logically and to achieve }
encapsulation. The outer class InnerClass {
class members (including //something
private) can be accessed by
}
the inner class.
}
Types of Inner classes
Local Inner Class

● It is a type of inner class that is defined inside a block. Here block


denotes a method body (a group of statements enclosed between a pair
of braces).
● These are the non-static classes because they can access the instance
members of the block.
● These classes must be instantiated in the block in which they are defined.
● When we compile the compiler generates the two class files namely
Outer.class and Outer$1Inner.class.
Local inner class example
Output and file list
Types of Inner classes
Anonymous Inner Class

● It is a type of inner class that is the same as local classes but the only
difference is that the class has not a class name and a single object is
created of the class. It makes the code more concise. There are two
ways.
○ By using an interface
○ By declaring the class concrete and abstract
Anonymous inner class example
Now that you know inner class, you may have…
Wrapper Class
● In Java, the term wrapper class represents a collection of Java classes
that objectify the primitive type of Java.
● It means that for each primitive type there is a corresponding wrapper
class. The wrapper classes are used to perform the conversion from a
primitive type to an object and vice-versa.
● To understand wrapper class hierarchy we first revisit the data type
hierarchy.
Java Data Type Tree
Java Wrapper Class Hierarchy and Options
Wrapper Classes Example (for JDK <=8)
Wrapper Classes Example (result and alternative)
Autoboxing and Unboxing

You might also like