[go: up one dir, main page]

0% found this document useful (0 votes)
10 views34 pages

Unit 1 Part 2 2024

The document provides an overview of classes and objects in Java, explaining that a class is a user-defined data type that serves as a blueprint for creating objects, which have state, behavior, and identity. It covers object creation, methods, constructors, and the use of the 'static' keyword, along with access specifiers that regulate visibility and access to class members. Examples illustrate concepts such as method overloading, constructor overloading, and the differences between constructors and methods.

Uploaded by

ansushobhana
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)
10 views34 pages

Unit 1 Part 2 2024

The document provides an overview of classes and objects in Java, explaining that a class is a user-defined data type that serves as a blueprint for creating objects, which have state, behavior, and identity. It covers object creation, methods, constructors, and the use of the 'static' keyword, along with access specifiers that regulate visibility and access to class members. Examples illustrate concepts such as method overloading, constructor overloading, and the differences between constructors and methods.

Uploaded by

ansushobhana
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/ 34

Classes & Objects in Java

Class in Java
• A class is a user-defined data type.
• A Class is collection of members like
Attributes(Variables) & Methods(Functions)
• It is a template or blueprint from which objects are
created.
• A class in java can contain:
– data members(variables) & methods
– constructors
– block
– class and interface
Object in Java
• An entity that has state and behavior is known as an
object. e.g. chair, bike, marker, pen,
• It can be physical or logical (touchable or non-touchable)
• The example of logical object is banking system.
• An object has three characteristics:
– state: represents data (value) of an object.
– behavior: represents the behavior (functionality) of an
object such as deposit, withdraw etc.
– identity: Object identity is typically implemented via a
unique ID. The value of the ID is not visible to the
external user.
• But, it is used internally by the JVM to identify each object
uniquely.
Object in Java
• For Example: Pen is an object.
– Its name is Reynolds,
– color is white etc. known as its state.
– It is used to write, so writing is its behavior.
General form to declare a class
Example of Class and Object
class Student_Info Output:
{ 0
int id;//data member null
String name;//data member
public static void main(String args[])
{
Student_Info s = new Student_Info();
System.out.println(s.id);
System.out.println(s.name);
}
}
Object Creation

• There are three steps when creating an object


from a class −
– Declaration − A variable declaration with a variable
name with an object type.
– Instantiation − The 'new' keyword is used to create the
object.
– Initialization − The 'new' keyword is followed by a call
to a constructor. This call initializes the new object.
Example…
class Student_Info
Output:
{ ID:1
int id;//data member Name: ABC
String name;//data member
public static void main(String args[])
{
Student_Info s = new Student_Info();
s.id = 1;
s.name = “ABC”
System.out.println(“ID:”+s.id);
System.out.println(“Name:”+s.name);
}
}
new Keyword
Indicates that it does not
point to any object null
s
Student s;
s = new Student( );
s.id
s
combined s.name
Student s = new Student();
Methods in Java
• In java, a method is like function i.e. used to
expose behavior of an object.
• Advantage of Method
– Code Reusability
– Code Optimization
Simple method…
class Bank
{
double p, r;
int n;
double simpleInterest()
{
return(p*r*n/100);
}
} class Bank_Demo
{
public static void main(String[] args)
{
Bank b1 = new Bank();
b1.p = 5000; b1.r = 12; b1.n = 5;
double si = b1.simpleInterest();
System.out.println(“Simple interest is:”+ si);
}
}
Parameterized method
class Bank
{
double p, r;
int n;
void setData(double pa, double ra, int ny)
{ p = pa; r = ra; n = ny; }
double simpleInterest()
{
return(p*r*n/100);
}
class Bank_Demo
}
{
public static void main(String[] args)
{ Bank b1 = new Bank();
Bank b2 = new Bank();
b1.setData(5000,12 ,5);
b2.setData(10000,12 ,5);
double si = b1.simpleInterest();
System.out.println(“Simple interest is:”+ si);
System.out.println(“Simple interest is:”+
b2. simpleInterest());
}
}
Method Overloading
• If a class has multiple methods having same name but
different Signature (parameters) known as Method
Overloading.

Different ways to overload the method


– By changing number of arguments
– By changing the data type of arguments
By changing the no. of arguments
class Calculation
{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(int a,int b,int c)
{
System.out.println(a+b+c);}
}
class CalDemo
{
public static void main(String args[])
{
Calculation c=new Calculation();
c.sum(10,10,10);
c.sum(20,20);
}
}
By changing data type….

class Calculation
{
void sum(int a,int b)
{
System.out.println(a+b); class CalDemo
} { public static void main(String args[])
void sum(double a, double b)
{
{
System.out.println(a+b); Calculation c=new Calculation();
} c.sum(10,20);
void sum(float a, float b) c.sum(20.5,30.5);
{ c.sum(10.5f,20.6);
System.out.println(a+b);
}
}
} }
Constructor in Java
• Constructor in java is a special type of method that is
used to initialize the object.
• Constructor is invoked at the time of object creation.
• It constructs the values(provides data) for the object that is
why it is known as constructor.

• Rules for creating java constructor


1. Constructor name must be same as its class name
2. Constructor must have no explicit return type

• Types of java constructors


1. Default constructor (no-argument constructor)
2. Parameterized constructor
Java Default Constructor
• A constructor that have no parameter is known as
default constructor.
• Default constructor provides the default values to
the object like 0, null etc. depending on the type.
• Syntax of default constructor:
<class_name>()
{
------------
}

• Rule: If there is no constructor in a class,


compiler automatically creates a default
constructor.
Example…
class Bank
{
double p, r;
int n;
Bank()
{
p = 5000; r = 12; n = 5;
}
double simpleInterest()
{
class Bank_Demo
return(p*r*n/100);
{
}
public static void main(String[] args)
}
{ Bank b1 = new Bank();
Bank b2 = new Bank();
double si = b1.simpleInterest();
System.out.println(“Simple interest is:”+ si);
si= b2. simpleInterest();
System.out.println(“Simple interest is:”+ si);
}
}
Parameterized Constructor
class Bank
{
double p,r;
int n;
Bank(double pa, double ra, int ny)
{
p = pa; r = ra; n = ny;
}
double simpleInterest()
{
class Bank_Demo
return(p*r*n/100);
{
}
public static void main(String[] args)
}
{ Bank b1 = new Bank(5000,12,5);
Bank b2 = new Bank(1000,12,4);
double si = b1.simpleInterest();
System.out.println(“Simple interest is:”+ si);
si= b2. simpleInterest();
System.out.println(“Simple interest is:”+ si);
}
}
Can’t Do…Compilation Error
class Student
{ int id; String name;
Student(int i,String n) { id = I; name = n; }
void display(){System.out.println(id+" "+name);}
public static void main(String args[])
{ Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");

Student s3 = new Student(); X


s1.display();
s2.display();
s3.display();
}
}
Constructor Overloading in Java
class Student
{
int id, age; void display()
String name; {
Student( ) System.out.println(id+" "+name+" "+age);
{ }
id = 0; name = ” ”; age=0 public static void main(String args[])
} {
Student(int i, String n) Student s1 = new Student(1,“XYZ");
{ Student s2 = new Student(2,"ABC",25);
id = i; name = n; age=18; Student s3 = new Student();
} s1.display();
Student(int i, String n, int a) s2.display();
{ s3.display();
id = i; }
name = n; }
age=a;
}
Use of this keyword
• this is a keyword in Java. Which can be used inside method or
constructor of class.
• It(this) works as a reference to current object whose method or
constructor is being invoked.
Example…(without this)
class Actor
{
String lastname;
String firstname;
Actor(String lastname, String firstname)
{
lastname = lastname;
firstname = firstname;
}
public static void main(String args[])
{
Actor a = new Actor("Devgan","Ajay");
System.out.println("Name of Actor:"+a.firstname
+" "+a.lastname);
}
}
Example…(with this keyword)
class Actor
{
String lastname;
String firstname;
Actor(String lastname, String firstname)
{
this.lastname = lastname;
this.firstname = firstname;
}
public static void main(String args[])
{
Actor a = new Actor("Devgan","Ajay");
System.out.println("Name of Actor:"+a.firstname
+" "+a.lastname);
}
}
Difference between constructor and method
Java Constructor Java Method

Constructor is used to initialize the state Method is used to expose behavior of an


of an object. object.

Constructor must not have return type. Method must have return type.

Constructor is invoked implicitly. Method is invoked explicitly.

The java compiler provides a default


Method is not provided by compiler in
constructor if you don't have any
any case.
constructor.

Constructor name must be same as the Method name may or may not be same
class name. as class name.
Static Keyword
• The static keyword in java is used for memory management mainly.
• A variable or method which is declared with static keyword also known as
Class variable or Class method because there is no need of object (instance)
to use these.
• The static can be:
– variable (also known as class variable)
– method (also known as class method)
– block
– nested class
• If any variable declared as static, it is known static variable.
• The static variable can be used to refer the common property of all objects
(that is not unique for each object) e.g. company name of employees, college
name of students etc.
• The static variable gets memory only once in class area at the time of class
loading.
Ex. of Counter without static
class Counter_Demo
{
int count=0;// will get memory when instance is created
Counter_Demo()
{
Output:
count++;
1
System.out.println(count); 1
} 1
public static void main(String args[])
{
Counter_Demo c1=new Counter_Demo();
Counter_Demo c2=new Counter_Demo();
Counter_Demo c3=new Counter_Demo();
}
}
Ex. of Counter with static
class Counter_Demo
{
static int count=0;//memory only once and retain its value
Counter_Demo()
{ count++;
System.out.println(count); Output:
} 1
public static void main(String args[]) 2
{ 3
Counter_Demo c1=new Counter_Demo();
Counter_Demo c2=new Counter_Demo();
Counter_Demo c3=new Counter_Demo();
}
}
Example of static variable
class Student
{
int rollno;
String name;
static String college =“Atmiya";
Student(int r,String n)
{
rollno = r; name = n;
}
void display ()
{
System.out.println(rollno+" "+name+" "+college);
}
public static void main(String args[])
{
Student s1 = new Student(10,”XYZ");
Student s2 = new Student(20,“ABC");
s1.display();
s2.display();
}
}
Java static method
• If you apply static keyword with any method, it is known as static
method.

• A static method can be invoked without the creating an instance


(object) of a class.

• static method can access static data member and can change the
value of it.

• Note: Static method can not access instance members of class.


Example…
class Calculate
{
static int cube(int x)
{ Output:
return x*x*x; 125
} 1000
}
class Demo
{
public static void main(String args[])
{
int result=Calculate.cube(5);
System.out.println(result);
result=Calculate.cube(10);
System.out.println(result);
}
}
Access Specifiers
• Java Access Specifiers (Visibility Specifiers ) regulate access to
classes, attributes and methods in Java.
• It determine whether attributes or methods in a class, can be used or
invoked by another method in another class or sub-class.
• Access Specifiers can be used to restrict access.
• Access Specifiers are part of object-oriented programming.

• Types Of Access Specifiers :


1. public
2. private
3. protected
4. default(no specifier)
Access Specifiers…

You might also like