[go: up one dir, main page]

0% found this document useful (0 votes)
6 views23 pages

lecture 3

The document provides a comprehensive overview of key concepts in Java, including objects, classes, and constructors. It explains the characteristics of objects, the structure of classes, and various types of constructors such as default, parameterized, and private constructors. Additionally, it covers the final keyword, access modifiers, and the concept of constructor overloading in Java.

Uploaded by

dayesal322
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)
6 views23 pages

lecture 3

The document provides a comprehensive overview of key concepts in Java, including objects, classes, and constructors. It explains the characteristics of objects, the structure of classes, and various types of constructors such as default, parameterized, and private constructors. Additionally, it covers the final keyword, access modifiers, and the concept of constructor overloading in Java.

Uploaded by

dayesal322
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/ 23

Index

• Methods & Classes


• constructor
Object in Java
• Object is an instance of a class. Class is a
template or blueprint from which objects are
created. So object is the instance(result) of a
class.
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.
Class in Java
• Class is a template or blueprint from which
objects are created.
A class in java can contain:
• data member
• method
• constructor
• block
• class and interface
Syntax to declare a class:
class <class_name>
{
data member;
method;
}
Constructor in Java
• Constructor is a special type of method whose
name is same as class name.
• Constructor in java that is used to initialize the
object.
• Every java class has a Constructor.
• Java constructor is called at the time of object
creation.
• It constructs the values i.e. provides data for
the object that is why it is known as
constructor.
Rules for creating java constructor
There are basically two rules defined for the
constructor.
• Constructor name must be same as its class
name
• Constructor must have no explicit return type
Types of java constructors
There are two types of constructors:
• Default constructor (no-arg constructor)
• Parameterized constructor
• Private constructor
• Copy constructor
Example of default constructor

• A constructor which does not have any parameter is called default constructor.
Class A
{ no any parameter
A()
{
}
}
class A
{
Int a; string b; boolean c;
A()
{
a=10; b=“rahul”; c=“true”;
}
void disp()
{
System.out.print(a b c)
}
}
Class B
public static void main(String args[])
{
A b=new A();
b.disp();
}
}
Rule: If there is no constructor in a class,
compiler automatically creates a default
constructor.
Example of parameterized constructor
class A {
int x,y;
A( int a, int b)
{
x =a; y=b;
}
void display()
{
System.out.println(x+" "+y);
}
}
class B
public static void main(String args[]){
A r =new A(10,20)
r.display();
}
}
Java Copy Constructor
• There is no copy constructor in java. But, we can copy the values of one object to another like copy constructor in C++.
• There are many ways to copy the values of one object into another in java. They are:
• By constructor
• By assigning the values of one object into another

Copy constructor:- whenever we pass object reference to the constructor then it is called copy constructor
//....................................................Example of copy constructor
class A {
int a, string b;
A()
{
a =10; b=“ansh”;
S.O.P(a+” ” +b);
}
A(A ref)
{
a=ref.a;
b=ref.b;
S.O.P(a+” ” +b);
}
}
class B
{
public static void main(String args[])
{
A r =new A();
A r2 =new A();
}
}
Private constructor:- it is possible to write a constructor as a private
but we cant access private constructor outside the class
//....................................................Example of Private constructor
class A
{
int a, double b, string c;
Private A()
{
a =10; b=30.56; c=“ansh”;
S.O.P(a+” ” +b ” ” +c);
}
}
public static void main(String args[])
{
A r =new A();
}
Constructor Overloading in Java
• Constructor overloading is a technique in Java
in which a class can have any number of
constructors that differ in parameter lists.
• The compiler differentiates these constructors
by taking into account the number of
parameters in the list and their type.
//..........constructor overloading
class A
{
int a, double b, string c;
A()
{
a =10; b=30.56; c=“ansh”;
}
A(int x)
{
a = x;
}
A(double y, string z)
{
b = y; c = z;
}
}
class B

public static void main(String args[])


{
A r =new A();
A r2 =new A(10);
A r3 =new A(23.76, ”ansh”);
S.O.P(r.a+” ” +r.b ” ” +r.c);
S.O.P(r2.a);
S.O.P(r3.b + ” ” +r3.c);
}
Java static method
If you apply static keyword with any method, it is known as static
method.
• A static method belongs to the class rather than object of a class.
• A static method can be invoked without the need for creating an
instance of a class.
• static method can access static data member and can change the
value of it.

class A
{
static
{
System.Out.Println(”raunak”);
}
public static void main(String args[])
Final Keyword In Java
The final keyword in java is used to restrict the
user. The java final keyword can be used in
many context. Final can be:
• variable
• method
• class
• The final keyword can be applied with the
variables, a final variable that value cant be
changed.
• It can be initialized in the constructor only.
• The blank final variable can be static also
which will be initialized in the static block only.
If you make any variable as final, you cannot change
the value of final variable(It will be constant).
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
Output:Compile Time Error
Java final method
If you make any method as final, you cannot override it.
class Bike{
final void run()
{
System.out.println("running");}
}
class Honda extends Bike{
void run()
{
System.out.println("running safely with 100kmph");
}
public static void main(String args[])
{
Honda honda= new Honda();
honda.run();
}
}
Output:Compile Time Error
Java final class
If you make any class as final, you cannot extend it.

final class Bike{}

class Honda1 extends Bike{


void run()
{
System.out.println("running safely with 100kmph");
}
public static void main(String args[])
{
Honda1 honda= new Honda();
honda.run();
}
}
Output:Compile Time Error
• A final variable that is not initialized at the
time of declaration is known as blank final
variable.
• If you want to create a variable that is
initialized at the time of creating object and
once initialized may not be changed, it is
useful.
Can we initialize blank final variable?
Yes, but only in constructor. For example:
class Bike10{
final int speedlimit;//blank final variable

Bike10()
{
speedlimit=70;
System.out.println(speedlimit);
}

public static void main(String args[]){


new Bike10();
}
}
Access Modifiers in Java
• The access modifiers in Java specifies the accessibility or scope of a
field, method, constructor, or class. We can change the access level
of fields, constructors, methods, and class by applying the access
modifier on it.
• There are four types of Java access modifiers:
• Private: The access level of a private modifier is only within the
class. It cannot be accessed from outside the class.
• Default: The access level of a default modifier is only within the
package. It cannot be accessed from outside the package. If you do
not specify any access level, it will be the default.
• Protected: The access level of a protected modifier is within the
package and outside the package through child class. If you do not
make the child class, it cannot be accessed from outside the package.
• Public: The access level of a public modifier is everywhere. It can
be accessed from within the class, outside the class, within the
package and outside the package.

You might also like