Classes and Objects in Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Classes and Objects in Java

Basics of Classes in Java

1
Some OOP concepts:

 Encapsulation
 Abstraction
 Information Hiding

2
Encapsulation : Wrapping up of data and methods into a single
unit is Encapsulation. In a class we combine data (variables )
and method that are operated on that data. (e.g. Class)

Abstraction : It is an act of representing only the essential


things without including background details. Like we are
providing only method declaration in interface not method
body (e.g. Interface)

Information Hiding : It is a concept that can be used for


hiding data from external world. And that can be achieved using
Encapsulation and Abstraction.
4
Classes
 A class is a collection of fields (data) and methods (procedure or
function) that operate on that data.

Circle

centre
radius

circumference()
area()

5
Classes
 A class is a collection of fields (data) and methods (procedure or
function) that operate on that data.
 The basic syntax for a class definition:

class ClassName{
[fields declaration]
[methods declaration]
}

 Bare bone class – no fields, no methods

public class Circle {


// my circle class
}
6
Adding Fields: Class Circle with fields
 Add fields

public class Circle {


public double x, y; // centre coordinate
public double r; // radius of the circle

 The fields (data) are also called the instance variables.

7
Adding Methods
 A class with only data fields has no life. Objects created
by such a class cannot respond to any messages.
 Methods are declared inside the body of the class but
immediately after the declaration of data fields.
 The general form of a method declaration is:

Return_type MethodName (parameter-list)


{
Method-body;
}
8
Adding Methods to Class Circle
public class Circle {

public double x, y; // centre of the circle


public double r; // radius of circle

//Methods to return circumference and area


public double circumference() {
return 2*3.14*r;
}
public double area() {
return 3.14 * r * r; Method Body

}
9
}
 Declare the Circle class, have created a new data type – User
Defined Data Type
 Can define variables (objects) of that type:

Circle aCircle = null;


Circle bCircle = null;

10
Class of Circle cont.
 aCircle, bCircle will have reference to Circle object, not an
object itself.

aCircle bCircle

null null

Points to nothing (Null Reference) Points to nothing (Null Reference)


11
Creating objects of a class
 Objects are created dynamically using the new keyword.
 aCircle and bCircle refer to Circle objects

aCircle = new Circle() ; bCircle = new Circle() ;

12
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;

bCircle = aCircle;

13
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;

bCircle = aCircle;

Before Assignment Before Assignment

aCircle bCircle aCircle bCircle

P Q P Q

14
Automatic garbage collection
Q
 The object does not have a reference and cannot be
used in future.

 The object becomes a candidate for automatic garbage


collection.

 Java automatically collects garbage periodically and releases


the memory used to be used in the future.

15
Accessing Object/Circle Data
 Similar to C syntax for accessing data defined in a structure.

ObjectName.VariableName
ObjectName.MethodName(parameter-list)

Circle aCircle = new Circle();

aCircle.x = 2.0 // initialize center and radius


aCircle.y = 2.0
aCircle.r = 1.0
16
Executing Methods in Object/Circle
 Using Object Methods:

sent ‘message’ to aCircle

Circle aCircle = new Circle();

double area;
aCircle.r = 1.0;
area = aCircle.area();

17
Using Circle Class
// Circle.java: Contains both Circle class and its user class
//Add Circle class code here
class MyMain
{
public static void main(String args[])
{
Circle aCircle; // creating reference
aCircle = new Circle(); // creating object
aCircle.x = 10; // assigning value to data field
aCircle.y = 20;
aCircle.r = 5;
double area = aCircle.area(); // invoking method
double circumf = aCircle.circumference();
System.out.println("Radius="+aCircle.r+" Area="+area);
System.out.println("Radius="+aCircle.r+" Circumference ="+circumf);
}
}
java MyMain
Radius=5.0 Area=78.5
18 Radius=5.0 Circumference =31.400000000000002
Packages
 The classes of the Java standard class library are organized into
packages

 Some of the packages in the standard class library are:

Package Purpose

java.lang General support


java.applet Creating applets for the web
java.awt Graphics and graphical user interfaces
javax.swing Additional graphics capabilities
java.net Network communication
java.util Utilities
javax.xml.parsers XML document processing
The import Declaration
 When you want to use a class from a package, you could use
its fully qualified name
java.util.Scanner

 Or you can import the class, and then use just the class name
import java.util.Scanner;

 To import all classes in a particular package, you can use the


* wildcard character
import java.util.*;
21
The import Declaration
 All classes of the java.lang package are imported
automatically into all programs

 It's as if all programs contain the following line:

import java.lang.*;

 That's why we didn't have to import the System or


String classes explicitly in earlier programs

 The Scanner class, on the other hand, is part of the


java.util package, and therefore must be imported

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