0% found this document useful (0 votes)
24 views22 pages

Java OOP: Methods and Constructors Guide

The document covers Object-Oriented Programming (OOP) principles, focusing on methods, constructors, and the structure of classes in Java. It explains the differences between built-in and user-defined methods, the role of constructors, and the use of keywords like 'new', 'static', and 'this'. Additionally, it provides examples of default and parameterized constructors, as well as constructor overloading.

Uploaded by

davegach31
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)
24 views22 pages

Java OOP: Methods and Constructors Guide

The document covers Object-Oriented Programming (OOP) principles, focusing on methods, constructors, and the structure of classes in Java. It explains the differences between built-in and user-defined methods, the role of constructors, and the use of keywords like 'new', 'static', and 'this'. Additionally, it provides examples of default and parameterized constructors, as well as constructor overloading.

Uploaded by

davegach31
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

OBJECT ORIENTED PROGRAMMING

RUAHA CATHOLIC UNIVERSITY


DEPT.: Computer Science
Course Code : RCS122
Lecturer: Mr. Karisa & Mr. Vincent

Module 6: Mastering Reusability Structure, and


Object Initialization
Methods and Constructors

Mastering Reusability Structure,


and Object Initialization
Learning Objectives
❖ Articulate the purpose and structure of methods in Java programming.
❖ Differentiate between built-in (standard library) methods and user-defined
methods.
❖ Demonstrate comprehension of static and non-static methods and their
appropriate applications.
❖ Evaluate the processes involved in defining, calling, and passing parameters
to methods.
❖ Explain the concept and role of constructors within Java.
❖ Apply the rules for creating constructors, including both default and
parameterized types.
❖ Compare and contrast the characteristics of constructors and methods in
Java.
OOP
❖Object-oriented programming (OOP) is a
programming paradigm that revolves around the
concept of objects, which can contain data
(attributes or properties) and code (methods or
functions).
❖OOP focuses on modeling real-world entities as
software objects, allowing for modular, reusable,
and maintainable code.
❖There are several key concepts in OOP
Objects and classes
❖Java, like many other programming languages, is
an object-oriented programming (OOP) language.
❖OOP revolves around the concept of objects,
which represent real-world entities, and classes,
which act as blueprints for creating those objects.
Class
❖It is a blueprint or a template that defines the
properties (attributes) and functionalities
(methods) that objects of that class will share.
❖You can think of a class as a cookie cutter for
creating objects.
❖Just like a cookie cutter shapes dough into a
specific form, a class defines the structure for
objects.
Object
❖ An object is an instance of a class.
❖ It's a concrete entity that holds the actual data (attribute values)
and implements the behavior (methods) defined by the class.
❖ Multiple objects can be created from the same class, each with
its own unique data.
❖ For example, if you have a Car class, you could create objects
for specific cars, like a red Honda Civic or a blue Ford F-150.
These objects would all have the properties defined by the Car
class, such as model, color, and year, but they would have their
own unique values for those properties.
“new” keyword
❖ The most common way to create an object in Java is by using
the new keyword followed by a call to a constructor.
Syntax:

ClassName objectName = new ClassName( );


Constructor

❖A constructor is a special method that is called


when an object of a class is created.
❖It initializes the object's state.
❖It has the same name as the class and doesn't
have a return type, not even void.
Constructor

class Person {
String name;
// Constructor
public Person(String n) {
name = n; }
}
// Creating an object and invoking the constructorPerson
person1 = new Person("Alice");
Methods

❖ Methods are functions defined within a class that


define the behavior of objects.
❖They are used to perform certain actions and can
manipulate the state of the object.
❖Methods can have parameters and return values.
Methods
Types of constructor

Default Constructor:
❖If a class doesn't have any constructor defined,
Java provides a default constructor.
❖It initializes member variables with default values
(e.g., 0 for numeric types, null for objects).
Types of constructor
class Student {
int id;
String name;
// No explicit constructor defined, so Java provides a
default constructor
}
// Creating an object and invoking the default constructor
Student student = new Student( );
Types of constructor

Parameterized Constructor:
❖A constructor with parameters is called a
parameterized constructor.
❖It allows initializing object properties with specific
values passed as arguments during object
creation.
Types of constructor
class Car {
String brand;
int year;
// Parameterized constructor
public Car (String b, int y) {
brand = b;
year = y;
}
}
// Creating an object and invoking the parameterized constructor
Car myCar = new Car ("Toyota", 2022);
Constructor overloading
class Book {
String title;
String author;
// Constructor with one parameter
public Book (String t) {
title = t;
}
// Constructor with two parameters
public Book (String t, String a) {
title = t;
author = a;
}
}
// Creating objects using different constructors
Book book1 = new Book ("Java Programming");
Book book2 = new Book ("Python Basics", "John Doe");
Static keyword
❖The static keyword defines class-level members
that belong to the class, not to individual objects.

❖They can be accessed using the class name


Static keyword
class Counter {
static int count = 0;
// Static method to increment count
public static void increment ( ) {
count++;
}
}
// Accessing static variable and method
[Link] ( ); // Incrementing count
int totalCount = [Link]; // Accessing count
This keyword
❖The this keyword refers to the current instance of
the class.
❖It is primarily used to distinguish between
instance variables and parameters with the same
name.
This keyword
class Rectangle {
int length;
int width;
// Parameterized constructor
public Rectangle (int length, int width) {
// Using 'this' to distinguish between instance variable and
parameter
[Link] = length;
[Link] = width;
}
}
The End
❖ Any Questions?, Any Clarifications?
✔ Feel free to Ask?
❖ Next: Module 7
✔ Arrays in Java
❖ Thank You..!

You might also like