0% found this document useful (0 votes)
6 views

PPL Lecture7

The document discusses object-oriented programming concepts like classes, objects, methods, constructors, this keyword, and garbage collection in Java. A class defines the structure and behavior of an object using variables and methods. Objects are instantiated from classes using the new keyword. Methods are used to define reusable blocks of code. Constructors initialize objects. The this keyword refers to the current object. Garbage collection automatically reclaims unused memory.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PPL Lecture7

The document discusses object-oriented programming concepts like classes, objects, methods, constructors, this keyword, and garbage collection in Java. A class defines the structure and behavior of an object using variables and methods. Objects are instantiated from classes using the new keyword. Methods are used to define reusable blocks of code. Constructors initialize objects. The this keyword refers to the current object. Garbage collection automatically reclaims unused memory.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SPPU Principles of Programming Notes

(Lecture 7)

Classes and Methods: class fundamentals


A class is a blueprint from which individual objects are created.
Following is a sample of a class.

Example
public class Dog {
String breed;
int age;
String color;

void barking() {
}

void hungry() {
}

void sleeping() {
}
}

A class can contain any of the following variable types.


• Local variables − Variables defined inside methods, constructors or
blocks are called local variables. The variable will be declared and
initialized within the method and the variable will be destroyed when
the method has completed.
• Instance variables − Instance variables are variables within a class
but outside any method. These variables are initialized when the
class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that class.
• Class variables − Class variables are variables declared within a
class, outside any method, with the static keyword.
A class can have any number of methods to access the value of various kinds of
methods. In the above example, barking (), hungry () and sleeping() are methods.

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


Declaring objects
As mentioned previously, a class provides the blueprints for objects. So basically,
an object is created from a class. In Java, the new keyword is used to create new
objects.
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.
Following is an example of creating an object −
Example

public class Puppy {


public Puppy (String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}

public static void main (String []args) {


// Following statement would create an object my Puppy
Puppy my Puppy = new Puppy( "tommy" );
}
}
If we compile and run the above program, then it will produce the following result −

Output
Passed Name is: tommy

Assigning Object Reference

We are assigning one object reference variable to another object reference


variable, we are not creating a copy of the object, we are only making a copy of
the reference. For example, in the following program, the object
references d1, d2 and d3 will point to the same object. Therefore, any change that
we make to the object referenced by d3 will also affect the same

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


objects d2 and d1 is referring. We can create multiple references to the same
object.

Example:

class Data {

int data1;

int data2;

public class Javaapp {

public static void main (String[] args) {

Data d1 = new Data();

d1.data1 = 20;

Data d2 = d1;

d2.data2 = 40;

Data d3 = d2;

System.out.println("d3.data1 : "+d3.data1);

System.out.println("d3.data2 : "+d3.data2);

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


Adding methods to a class

A method is a block of code or collection of statements or a set of code grouped


together to perform a certain task or operation. It is used to achieve
the reusability of code. We write a method once and use it many times. We do
not require to write code again and again. It also provides the easy
modification and readability of code, just by adding or removing a chunk of
code. The method is executed only when we call or invoke it.

Method Declaration

The method declaration provides information about method attributes, such as


visibility, return-type, name, and arguments. It has six components that are known
as method header, as we have shown in the following figure.

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


Constructor

In Java, a constructor is a block of codes similar to the method. It is called when


an instance of the class is 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.

Types of Constructors
There are two types of constructors in Java:

1. Default constructor (no-arg constructor)


2. Parameterized constructor

Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.

Syntax
<class name>(){}
Example:
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of obj

class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


Parameterized Constructor
A constructor which has a specific number of parameters is called a parameterized
constructor.

Why use the parameterized constructor?

The parameterized constructor is used to provide different values to distinct


objects. However, you can provide the same values also.

Example:

In this example, we have created the constructor of Student class that have two
parameters. We can have any number of parameters in the constructor.

//Java Program to demonstrate the use of the parameterized constructor.

class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int I, String n) {
id = i;
name = n;
}
//method to display the values
void display (){System.out.println(id+" "+name);}

public static void main (String args[]){


//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1. display ();
s2. display();
}

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


This Keyword
There can be a lot of usage of Java this keyword. In Java, this is a reference
variable that refers to the current object.

Example:
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
rollno=rollno;
name=name;
fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis1{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


Garbage Collection
Garbage Collection is process of reclaiming the runtime unused memory
automatically. In other words, it is a way to destroy the unused objects.

To do so, we were using free () function in C language and delete() in C++. But,
in java it is performed automatically. So, java provides better memory
management.

Advantage of Garbage Collection


o It makes java memory efficient because garbage collector removes the
unreferenced objects from heap memory.
o It is automatically done by the garbage collector(a part of JVM) so we
don't need to make extra efforts.

Example:

public class TestGarbage1{


public void finalize
(){System.out.println("object is garbage collected");}
public static void main (String args[]){
TestGarbage1 s1=new TestGarbage1();
TestGarbage1 s2=new TestGarbage1();
s1=null;
s2=null;
System.gc();
}
}

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy