Module 2 Chapter 2
Module 2 Chapter 2
com/c/EDULINEFORCSE
STUDENTS
CHAPTER 2
Class Fundamentals
Classes and Objects
• Classes and objects are the two main aspects of object-oriented
programming.
Create a Class
• To create a class, use the keyword class
Create an Object
• To create an object of MyClass, specify the class name, followed by
the object name, and use the keyword new
Multiple Objects
• You can create multiple objects of one class
Output
101 sonoo
Accessing Attributes
• We can access attributes by creating an object of the class, and by
using the dot syntax (.)
Example
Create an object called "myObj" and print the value of x
Output
5
Modify Attributes
• We can also modify attribute values
Example
Set the value of x to 40
Output
40
Output
25
If you don't want the ability to override existing values, declare
the attribute as final
Multiple Objects
If we create multiple objects of one class, you can change the
attribute values in one object, without affecting the attribute
values in the other
Example - Change the value of x to 25 in myObj2, and leave x in
myObj1 unchanged
Multiple Attributes
We can specify as many attributes as you want
Example
Inside main, call myMethod()
Example
The differences between static and public methods
Remember that..
• The dot (.) is used to access the object's attributes and methods.
• To call a method in Java, write the method name followed by a set
of parentheses (), followed by a semicolon (;)
Using Multiple Classes
• It is a good practice to create an object of a class and access it in
another class.
• 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:
Car.java
OtherClass.java
Prepared By Mr. EBIN PM, AP, IESCE
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
Types of Java constructors
Default constructor (no-argument constructor)
Parameterized constructor
Output
Bike is created
The constructor name must match the class name, and it cannot
have a return type (like void).
The constructor is called when the object is created.
All classes have constructors by default
If you do not create a class constructor yourself, Java creates one
for you. However, then you are not able to set initial values for
object attributes.
Method Overloading
• With method overloading, multiple methods can have the same
name with different parameters
• Method overloading is one of the ways that Java supports
polymorphism.
There are two ways to overload the method in java
By changing number of arguments
By changing the data type
Example
RECURSION
• Recursion is the technique of making a method call itself.
• This technique provides a way to break complicated problems
down into simple problems which are easier to solve.
Syntax
Working
Example
Use recursion to add all of the
numbers up to 10.
Working
Example
Factorial of a number
THIS KEYWORD
There can be a lot of usage of java this keyword. In java, this is a
reference variable that refers to the current object.
Usage of java this keyword
• this can be used to refer current class instance variable.
• this can be used to invoke current class method (implicitly)
• this() can be used to invoke current class constructor.
• this can be passed as an argument in the method call.
• this can be passed as argument in the constructor call.
Output
Output
Output
Command-Line Arguments
• Sometimes we want to pass information into a program when we
run it. This is accomplished by passing command-line arguments
to main( ).
• The main method can receive string arguments from the
command line
• To access the command-line arguments inside a Java program is
quite easy— they are stored as strings in a String array passed to
the args parameter of main( ).
• The first command-line argument is stored at args[0], the second
at args[1], and so on.
Prepared By Mr. EBIN PM, AP, IESCE