Notes Unit 3
Notes Unit 3
UNIT-3
OBJECT ORIENTED PROGRAMMING CONCEPTS
3.1 CLASS AND OBJECT
o Fields / Variables
o Methods
o Constructors
o Blocks
o Nested class and interface
class class_name{
// field;
// method;
}
An entity that has state and behaviour is known as an object e.g., chair, bike, marker, pen, table, car, etc. It
can be physical or logical (tangible and intangible). The example of an intangible object is the banking
system.
Example: Pen is an object. Its name is Reynolds; colour is white, known as its state. It is used to write, so
writing is its behaviour.
Object Definitions:
class Student {
int id;
String name;
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student ();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
Output:
0
null
1.Default Constructor:
A default constructor is provided by Java if you do not explicitly define any constructor in your class. It has
no parameters and does not perform any initialization. Its purpose is to initialize the object with default
values (e.g., setting numeric properties to 0 and reference properties to null).
EXAMPLE:
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
JAVA PROGRAMMING
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
Output :
Bike is created
2. Parameterized Constructor:
A parameterized constructor is a constructor that takes one or more parameters. It allows you to initialize the
object with specific values at the time of creation.
EXAMPLE:
class Person(var name: String, var age: Int) {
// Parameterized constructor
init {
println("Name: $name, Age: $age")
}
3.Copy Constructor:
A copy constructor is a constructor that creates a new object by copying the state of another object of the
same class. It is useful when you want to create a new object with the same values as an existing object.
EXAMPLE:
class Person(var name: String, var age: Int) {
// Primary constructor
constructor(person: Person) : this(person.name, person.age) {
// Copy constructor implementation
println("Copy constructor called")
}
// Method to display person details
fun displayDetails() {
println("Name: $name, Age: $age")
}
}
fun main()
{
// Creating a Person object
val person1 = Person("John", 30)
// Constructor
public MyClass(int value) {
this.value = value; // Assigning value to the instance variable
}
// Main method
public static void main(String[] args) {
// Creating an instance of MyClass
MyClass obj = new MyClass(10);
// Calling the method to display value
obj.displayValue();
}
}
JAVA PROGRAMMING
// Primary constructor
public MyClass(int value) {
this.value = value;
System.out.println("Primary constructor invoked with value: " + value);
}
// Main method
public static void main(String[] args) {
// Constructor
public MyClass() {
count++; // Incrementing static variable in the constructor
}
// Main method
public static void main(String[] args) {
2.Static Methods:
When a method is declared as static, it is called a static method. Static methods belong to the class rather
than individual objects. They can be invoked directly on the class itself, without the need to create an
instance.
EXAMPLE:
public class MyClass {
// Static method
public static void staticMethod() {
System.out.println("This is a static method");
}
// Main method
public static void main(String[] args) {
// Calling static method using class name
MyClass.staticMethod();
}
}
3.Static Block:
A static block is a block of code enclosed in curly braces {} that is executed when the class is loaded into
memory. It is used to initialize static variables or perform any other static initialization tasks.
EXAMPLE:
public class MyClass {
// Static variable
public static int count;
// Static block
static {
// Initializing static variable in the static block
count = 0;
System.out.println("Static block initialized");
}
// Main method
public static void main(String[] args) {
// Accessing the static variable
System.out.println("Count: " + MyClass.count); } }
JAVA PROGRAMMING
2.Concatenation:
The concat() method concatenates two strings and returns a new string.
String str1 = "Hello";
String str2 = "World";
String result = str1.concat(str2);
Output: result = "HelloWorld"
Alternatively, you can use the + operator for string concatenation.
String result = str1 + str2;
Output result = "HelloWorld"
3.Substring:
The substring() method returns a substring of the original string based on the specified starting and ending
indexes.
String str = "Hello, World!";
String substr = str.substring(7, 12);
Output: substr = "World"
4.Comparison:
The equals() method checks if two strings have the same content.
String str1 = "Hello";
String str2 = "Hello";
boolean isEqual = str1.equals(str2);
Output: isEqual = true
The equalsIgnoreCase() method compares strings while ignoring case differences.
JAVA PROGRAMMING
5.Conversion:
The toUpperCase() and toLowerCase() methods convert the string to uppercase and lowercase,
respectively.
String str = "Hello, World!";
String uppercase = str.toUpperCase();
String lowercase = str.toLowerCase();
Output: uppercase = "HELLO, WORLD!"
lowercase = "hello, world!"
6.Searching:
The indexOf() method returns the index of the first occurrence of a specified substring within a string.
String str = "Hello, World!";
int index = str.indexOf("World");
Output: index = 7
7.Splitting:
The split() method splits a string into an array of substrings based on a specified delimiter.
String str = "Hello,World,Java";
String[] parts = str.split(",");
Output: parts = ["Hello", "World", "Java"]
8.Replacement:
The replace() method replaces all occurrences of a specified character or substring with another character or
substring.
String str = "Hello, World!";
String replaced =str.replace("Hello", "Hi");
Output: replaced = "Hi, World!"
JAVA PROGRAMMING
EXAMPLE:
public class StringOperationsExample {
public static void main(String[] args) {
// Length
String str = "Hello, world!";
int length = str.length();
System.out.println("Length of the string: " + length);
// Concatenation
String str1 = "Hello";
String str2 = "World";
String concatenatedString = str1.concat(" ").concat(str2);
System.out.println("Concatenated string: " + concatenatedString);
// Substring
String substring = str.substring(7);
System.out.println("Substring from index 7: " + substring);
// Comparison
String str3 = "hello";
boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str3);
System.out.println("Comparison (ignoring case): " + isEqualIgnoreCase);
// Conversion
int number = 123;
String strNumber = String.valueOf(number);
System.out.println("Converted number to string: " + strNumber);
// Searching
boolean contains = str.contains("world");
System.out.println("Contains 'world': " + contains);
JAVA PROGRAMMING
// Splitting
String sentence = "This is a sentence";
String[] words = sentence.split("\\s+");
System.out.println("Splitting sentence into words:");
for (String word : words) {
System.out.println(word);
}
// Replacement
String replacedString = str.replace("world", "Java");
System.out.println("Replaced 'world' with 'Java': " + replacedString);
}
}
Difference between String and String Buffer
No. String StringBuffer
1)
The String class is immutable. The StringBuffer class is mutable.
5)
String class uses String constant pool. StringBuffer uses Heap memory
JAVA PROGRAMMING