Oopj Unit-5
Oopj Unit-5
with JAVA
Anand Kumar , Assistant Professor
Computer Science & Engineering
UNIT-5
Object oriented programming:
Outline
Introduction of Classes and objects: concepts of classes and objects
Declaring objects
Assigning object reference variables
Methods
Constructors
Access control
Garbage collection,
usage of static with data and methods
usage of final with data
Overloading methods and constructors,
Parameter passing - call by value,
Recursion
Nested classes.
Classes and Object
Introduction to Class and object
Introduction to Class and object
Introduction to Class and object
Introduction Of Class and Object
Class
Note: Variables & Methods naming conventions are same. But methods
will have parenthesis ( ( ) ) variables will not have parenthesis.
class Student {
{ public static void main(String [ ] args)
private int roll; {
private char grade; Student s=new Student( );
private float per; s.setData();
public void setData( ) s.showData();
{ }
roll=10; }
grade=‘A’l
per=66.5f;
}
public void showData( )
{
S.O.P(“Roll, grade and percentage is
”+roll,grade,per);
}
}
class Use
Initializing Data Member at Runtime
public void showDa )
{
S.O.P(“Roll is ”+roll);
S.O.P(“Grade is ”+grade);
S.O.P(“Percentage is ”+per);
}
}
class UseStudent
{
public static void main(String [ ] args)
{
Student s=new Student( );
s.setData();
s.showData( );
}
}
Creating Parameterized Methods
class Student
{
private int roll;
private char grade;
private float per;
public void setData(int r, char g, float p)
{
roll=r;
grade=g;
float=p;
}
public void showData( )
{
S.O.P(“Roll is ”+roll +“\nGrade is ”+grade+“\nPercentage is ”+per);
}
}
cla) Creating Parameterized Methods
{
Scanner kb=new Scanner(System.in);
Student s=new Student( );
S.O.P(“Enter roll, grade and percentage ”);
int roll=kb.nextInt( );
char grade=kb.next( );
float per=kb.nextFloat( );
s.setData(roll,grade,per);
s.showData( );
}
}
Constructors in Java
In Java, a constructor is a block of codes similar to the method. It is
called when an instance of the class created.
At the time of calling constructor, memory for the object is allocated
in the memory.
It is a special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least one
constructor is called.
It calls a default constructor if there is no constructor available in the
class. In such case, Java compiler provides a default constructor by
default.
There are two types of constructors in Java: no-arg constructor, and
parameterized constructor.
• Rules for creating Java constructor
There are two rules defined for the constructor.
Default Constructor
In Java, a constructor is said to be default constructor if it does not
have any parameter. Default constructor can be either user defined or
provided by JVM.
Constructor
If a class does not contain any constructor, then during runtime JVM
generates a default constructor which is known as system define
default constructor.
If a class contain a constructor with no parameter, then it is known as
default constructor defined by user. In this case JVM does not create
default constructor
• Object class is the Super/Parent class of every class. It is super daddy class.
• Object class has 8 methods in it. Hence, every class has at least 8 methods.
2. static methods
3. static blocks
4. static classes(Can be used only with nested class or inner class and not the outer class)
“static” Data members
• Usually, a non static data members is allocated in RAM only when an object is
created.
• static members are saved in RAM once, i.e. they are independent of the objects.
• A data member is made static when it should display same number change
for all objects.
Both a and b will get space in memory when
• For example, Object of class Data gets created.
class Data
{
int a; What if b is made static???
int b;
}
class Data
Objects and Classes
Static member
{
Class member
int a;
static int b; Shared member
} a 30,40 a
class UseData
{ 10 b 20
public static void main(String [ ] args)
{
Data d1=new Data( );
Data d2=new Data( );
d1.a=10;
d1 d2
d2.a=20;
System.out.println(d1.a+“\n”+d2.a); RAM
d1.b=30;
d2.b=40;
System.out.println(d1.b+“\n”+d2.b);
}
}
Features of “static” Data member
• Since, they are object independent they should be accessed using class name.
Data.b=30;
Data.b=40;
• It’s value will remain unchanged. Remember the data member Math.PI, it is declared
the same way. They behave like constants.
“final” Data Members
• The initialization of final data member can either be done explicitly while declaring or through con-
structors at run time only once. But in all the constructors defined in a class.
• Once explicitly initialized at declaration then it cannot be initialized again using constructors.
• Java strongly recommends that when any data members is both final and static in nature then it
should be named in upper case. Example, private final static double PI=3.14159.
It’s not a rule but a professional coding convention.
Example
class Data class Data
{ {
final int a; final static int a;
public Data(int static
x) {
{ a=10;
a=x; }
} public static void main(String
} [ ] args)
{
System.out.println(A.a);
}
“final” Methods
• final is used with methods whose functionality remains same throughout i.e. in base as
well as derived classes.
• Methods which are declared finals cannot be overridden in derived classes. But they
do get inherited.
• abstract methods can be called using derived class object but cannot be
overridden.
• For example, class String is a final class and no other class can inherit it.
• Classes are made final in cases where the data member or methods are sensitive
enough that they should not be altered at any cost.
• Is there any other way through which we can prevent a class from being inherited???
(Just for knowledge!)
“final” Classes
final class A
{
----
----
}
class B extends A
{
----
----
}
“final” Classes
• Though, final classes cannot be inherited but final classes can inherit other classes.
• For example, every class does inherit the class Object and hence a final class also
does inherit the class Object.
Method Overloading in Java
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
changing data type of arguments
class Adder{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
Constructor overloading in Java
In Java, we can overload constructors like methods. The constructor
overloading can be defined as the concept of having more than one
constructor with different parameters so that every constructor can
perform a different task.
public class Student {
//instance variables of the class
int id;
String name;
Student(){
System.out.println("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
Program Count…
public static void main(String[] args) {
Output:
Default Constructor values:
Student Id : 0
Student Name : null
Parameterized Constructor values:
Student Id : 10
class Operation{
int data=50;
int fact(int n)
{ int result;
if (n == 1) return 1;
result = fact(n - 1) * n;
return result;
}
Program
class Recursion {
{
GFG f = new GFG();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 4 is " + f.fact(4));
System.out.println("Factorial of 5 is " + f.fact(5));
}
}
Nested Class
Java inner class or nested class is a class that is declared
inside the class or interface.
If a class is declared with in another class, then this concept is
called as Inner classes or nested classes
Syntax
class A{
Class B{
}
}
Nested Class
If a class contains other class, then it is called as outer class or top-level class
Class declared inside the outer class is called as inner class
Thank You!!!
www.paruluniversi
ty.ac.in