0% found this document useful (0 votes)
38 views20 pages

OOP1 Unit4

Uploaded by

makkakuldip
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)
38 views20 pages

OOP1 Unit4

Uploaded by

makkakuldip
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/ 20

3140705 - Object Oriented Programming 1

Unit -4
Objects and Classes
• Java is an object-oriented programming language.

• Everything in Java is associated with classes and


objects, along with its attributes and methods.

• For example: in real life, a car is an object. The car


has attributes, such as weight and color, and
methods, such as drive and brake.
Create an Object
• In Java, an object is created from a class.

• To create an object of Class, specify the class name,


followed by the object name, and use the keyword new:

public class Main


{
int x = 5;
public static void main(String[] args)
{
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Multiple Objects
You can create multiple objects of one class:
public class Main
{
int x = 5;
public static void main(String[] args)
{
Main myObj1 = new Main(); // Object 1
Main myObj2 = new Main(); // Object 2
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
}
Using Multiple Classes
• You can also create an object of a class and access it
in another class.

• This is often used for better organization of classes


(one class has all the attributes and methods, while
the other class holds the main() method (code to be
executed)).

• Remember that the name of the java file should


match the class name.

• In this example, we have created two files in the


same directory/folder:
public class Main
{
int x = 5;
}
class Second
{
public static void main(String[] args)
{
Main myObj = new Main();
System.out.println(myObj.x);
}
}
When both files have been compiled:

C:\Users\Your Name>javac Main.java


C:\Users\Your Name>javac Second.java

Run the Second.java file:

C:\Users\Your Name>java Second

And the output will be:

5
Java Class Methods
• You learned that methods are declared within a class, and that
they are used to perform certain actions:

public class Main


{
static void myMethod()
{
System.out.println("Hello World!");
}
public static void main(String[] args)
{
myMethod();
}
}

// Outputs "Hello World!"


Static vs. Non-Static
• You will often see Java programs that have either
static or public attributes and methods.

• In the example above, we created a static


method, which means that it can be accessed
without creating an object of the class, unlike
public, which can only be accessed by objects:

• An example to demonstrate the differences


between static and public methods:
public class Main
{
static void myStaticMethod()
{
System.out.println("Static methods can be called without creating objects");
}
public void myPublicMethod()
{
System.out.println("Public methods must be called by creating objects");
}
public static void main(String[] args)
{
myStaticMethod(); // Call the static method
// myPublicMethod(); This would compile an error

Main myObj = new Main(); // Create an object of Main


myObj.myPublicMethod(); // Call the public method on the object
}
}

Output
Static methods can be called without creating objects
Public methods must be called by creating objects
Access Methods With an Object

public class Main


{
public void fullThrottle()
{
System.out.println("The car is going as fast as it can!");
}
public void speed(int maxSpeed)
{
System.out.println("Max speed is: " + maxSpeed);
}
public static void main(String[] args)
{
Main myCar = new Main(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method
}
}

Output
The car is going as fast as it can!
Max speed is: 200
Constructors

• A constructor in Java is a special method that is used


to initialize objects.

• The constructor is called when an object of a class is


created. It can be used to set initial values for object
attributes.

• Every time an object is created using the new()


keyword, at least one constructor is called.

• It calls a default constructor if there is no constructor


available in the class.
Rules for creating Java constructor
• There are two rules defined for the constructor.

• Constructor name must be the same as its class name

• A Constructor must have no explicit return type

• A Java constructor cannot be abstract, static, final, and


synchronized

Types of Java constructors


There are two types of constructors in Java:
– Default constructor (no-arg constructor)
– Parameterized constructor
Java Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.

Syntax of default constructor: class_name( ){ }

class Bike1
{
Bike1()
{
System.out.println("Bike is created");
}
public static void main(String args[])
{
Bike1 b=new Bike1();
}
}
Output: Bike is created

Rule: If there is no constructor in a class, compiler automatically creates a default


constructor.
What is the purpose of a default constructor?
The default constructor is used to provide the default values to the object like 0, null, etc.,
depending on the type.

class Student
{
int id;
String name;
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.display();
s2.display();
}
}
Output:
0 null
0 null
Java 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.
//Java Program to demonstrate the use of the parameterized constructor.

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");
s1.display();
s2.display();
}
}

Output:
111 Karan
222 Aryan
Constructor Overloading in Java

• In Java, a constructor is just like a method but without


return type.

• It can also be overloaded like Java methods.

• Constructor overloading in Java is a technique of having


more than one constructor with different parameter lists.

• They are arranged in a way that each constructor performs


a different task.

• They are differentiated by the compiler by the number of


parameters in the list and their types.
Example of Constructor Overloading
//Java program to overload constructors
class Student5{
int id;
String name;
int age;
//creating two arg constructor
Student5(int i,String n){
id = i;
name = n;
}
//creating three arg constructor
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}

public static void main(String args[]){


Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
}

Output:

111 Karan 0
222 Aryan 25

You might also like