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

OOP Unit-I Constructors Methods Packages

Uploaded by

suganyatklncecs
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OOP Unit-I Constructors Methods Packages

Uploaded by

suganyatklncecs
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

CS8392-Object

Oriented Programming
UNIT - I

INTRODUCTION TO OOP AND JAVA


FUNDAMENTALS

Prepared by,
Mrs. T. SUGANYA,
M.E.,
Constructor
Constructor:
◦ Method – whose name is similar to that
of class
◦ Constructor is a block of code that
initializes the newly created object
◦ Invoked immediately when an object is
created
◦ Implicit call
◦ It doesn’t return any value even void
◦ Return type of constructor is Class Type
itself
◦ Allows object to initialize themselves
Constructor
 Create a constructor Method
classname()
{ }
 Invoke Constructor implicitly
classsname object= new
classname()
Eg: class student
{
String name;
student()
{ name=“abc”;}
public static void main(String args[])
{
student s1=new student();
}
}
Types of Constructor
There are 2 types of constructors:
◦ Default /No-argument
◦ Parameterized
Default / No-arg
◦ Constructor with no arguments
Parameterized
 Constructor with arguments
Default Constructor
class Bike1
{
//creating a default constructor
Bike1()
{

System.out.println("Bike is created");
}
public static void main(String args[])
{
//calling a default constructor
Bike1 b=new Bike1();
}
}
Default Constructor
Ifthere is no constructor in a class,
compiler automatically creates a
default constructor.
Parameterized
Constructor
- used to provide different values to distinct
class
objects.Example3
{
private int var;
public Example3(int num)
{ var=num; }
public int getValue()
{ return var; }
public static void main(String
args[])
{
Example3 myobj = new Example3();
System.out.println("value of var is:
"+myobj.getValue());
}
Parameterized
Constructor

C:\jdk-14.0.2\bin>javac
Example3.java

C:\jdk-14.0.2\bin>java Example3
value of var is: 5
Parameterized
Constructor
- used to provide different values to distinct
class
objects.Example3
{
private int var;
public Example3(int num)
{ var=num; }
public int getValue()
{ return var; }
public static void main(String
args[])
{
Example3 myobj = new Example3(5);
System.out.println("value of var is:
"+myobj.getValue());
}
Parameterized
Constructor
Class with more than
one constructor
Main Memory
b1 height
box() 10
width
10
depth
10
Volume(){}
Main Memory
b2 height 5
box(5,5,
width 5
5) depth 5
Volume(){}
Deallocation
Garbage Collection
objects are dynamically allocated by
new operator
Java deallocates the objects
automatically. Garbage Collection is a
techniques that accomplishes this

Finalize Method
•If you to specify an action that will occur
during deallocation then you can use
finalize method
•Finalize method is invoked when objects
are destructed
Method Overloading
It is possible to have two or methods
with same name

Method Overloading
When 2 or more methods having
same name with different signature
( parameter, return type) then it is said to
be Method Overloading

Constructor is a method
Constructor can be overloaded
Eg:
box(){}
box(int x,int y,int z){}
Method Overloading
class box
{
area(int a)
{System.out.print("\n\t Area of Square =\
t\t"+(a*a)); }
area(int a,int b)
{System.out.print("\n\t Area of Rect =\t\
t"+(a*b)); }
}
public class ex1
{
public static void main(String args[])
{
box b1=new box();
b1.area(10);
b1.area(10,10);
b1.area(5); }}
Constructor Overloading
class box
{
double height,width,depth;
box()
{height=10; width=10; depth=10;
}
box(double x,double y,double depth)
{
height=x; width=y;
depth=z;

}
double volume()
{ return (height * width * depth); }

public class ex
{
public static void main(String args[])
{
box b1=new box();
System.out.print("\n\tVolume=\t\t"+b1.volume());
box b2=new box(5,5,5);
Package
Is a collection of classes and
interfaces
Act as a folder

Merits:
◦ Java package is used to categorize the classes and
interfaces so that they can be easily maintained.
◦ Java package provides access protection.
◦ Java package removes naming collision.
◦ Packages provide reusability of code .
◦ We can crate our own Package or extend already
available Package.
Types of Packages
Built in Package
◦ java.io I/O streams,scanners
◦ java.net socket, HttpCookie
◦ java,awt buttons,controls
◦ java.applet applet (client side
app)
◦ java.util
datastructures,date,time
◦ java.lang string,
exception,thread
◦ java.sql connection, driver
manager
User defined Package
◦ User can create their own packages using
package keyword
package packagename
import package;
Step 1:
Create a package
◦ Create a new folder in jdk\bin
Step2:
Set package name
◦ Mypack is the package name
Step 3:
Create a class within the package
◦ Save the class in Mypack package
Step

3:
Compile the class
◦ Quit the Editor
◦ Type the command,
C:\java\jdk\bin> javac Mypack\factorial.java
Before Compilation

After Compilation
STEP 4:
 Import the package
 Createa class to import the package Mypack
 Compile & run the class

You might also like