Course Title: Advanced Java Programming
Course Code: CSC409
Program: CSIT
Year/Semester-III/VII
Prepared By: Shyam Sunder Khatiwada
Fundamental of classes: Object
An object is any entity that has a state and behavior. For example, a
bicycle is an object. It has
● States: idle, first gear, etc
● Behaviors: braking, accelerating, etc.
Fundamental of classes : Object
An object is an instance of a class. A class is a template or blueprint from which
objects are created. So, an object is the instance(result) of a class.
Object Definitions:
● An object is a real-world entity.
● An object is a runtime entity.
● The object is an entity which has state and behavior.
● The object is an instance of a class.
Fundamentals of Classes:Class
A class is a group of objects which have common properties. It is a template or blueprint from
which objects are created. It is a logical entity. It can't be physical.
A class in Java can contain:
● Fields
● Methods
● Constructors
● Blocks
● Nested class and interface
Types of classes in Java
Built-in Classes
Built-in classes are just the predefined classes that come along with the Java
Development Kit (JDK). We also call these built-in classes libraries. Some
examples of built-in classes include:
● java.lang.System
● java.util.Date
● java.util.ArrayList
● java.lang.Thread
User-defined classes
● User-defined classes are rather
self-explanatory.
● The name says it all. They are
classes that the user defines and
manipulates in the real-time
programming environment.
● User-defined classes are broken
down into three types
Concrete class
Concrete class is just another standard class that the user defines and stores the
methods and data members in.
Syntax:
class con{
//class body;
}
Abstract Class
Abstract classes are similar to concrete classes, except that you need to define them using the "abstract"
keyword. If you wish to instantiate an abstract class, then it should include an abstract method in it.
Otherwise, it can only be inherited.
Syntax:
abstract class AbstClas{
//method();
abstract void demo();
}
Interfaces
Interfaces are similar to classes. The difference is that while class describes an
object’s attitudes and behaviors, interfaces contain the behaviors a class
implements. These interfaces will only include the signatures of the method but not
the actual method.
Rules for creating class
The following rules are mandatory when you're working with Java classes:
● The keyword "class" must be used to declare a class
● Every class name should start with an upper case character, and if you intend to
include multiple words in a class name, make sure you use the camel case
● A Java project can contain any number of default classes but should not hold
more than one public class
● You should not use special characters when naming classes
● You can implement multiple interfaces by writing their names in front of the
class, separated by commas
● You should expect a Java class to inherit only one parent class
Fundamentals of Classes: Creating Class
We can create a class in Java using the class keyword. For example,
class ClassName {
// fields
// methods
}
Here, fields (variables) and methods represent the state and behavior of the object respectively.
● fields are used to store data
● methods are used to perform some operations
Fundamentals of Classes: Creating Class
For our bicycle object, we can create the class as
In the above example, we have created a class named Bicycle. It contains a field named gear and a method named
braking().
Here, Bicycle is a prototype. Now, we can create any number of bicycles using the prototype. And, all the bicycles
will share the fields and methods of the prototype.
Java Objects: Creating an object
An object is called an instance of a class. For example, suppose Bicycle is a class then MountainBicycle,
SportsBicycle, TouringBicycle, etc can be considered as objects of the class.
Here is how we can create an object of a class.
We have used the new keyword along with the constructor of the class to create an object. Constructors are similar to
methods and have the same name as the class. For example, Bicycle() is the constructor of the Bicycle class.
Difference between object and class
Here,
Object we
and are
Classcreating
Example:a main
main() method
within inside the class.
the class
Object and class Example: main outside the class
In real time development, we create classes and
use it from another class. It is a better approach
than previous one. Let's see a simple example,
where we are having main() method in another
class.
Ways to initialize the object
There are three ways to initialize the object-
1. By reference variable
2. By constructor
3. By methods.
Initializing object using reference variable
● Initializing an object means storing data
into the object.
● We can also create multiple objects and
store information in it through reference
variable.
● //Creating objects
● Student s1=new Student();
● Student s2=new Student();
● //Initializing objects
● s1.id=101;
● s1.name="Sonoo";
● s2.id=102;
● s2.name="Amit";
● //Printing data
● System.out.println(s1.id+" "+s1.name);
● System.out.println(s2.id+" "+s2.name);
Initializing object using method
● In this example, we are creating
the two objects of Student class
and initializing the value to these
objects by invoking the
insertRecord method.
● Here, we are displaying the state
(data) of the objects by invoking
the displayInformation() method.
Initializing object using Constructor
● Constructor is used to initialize the object.
● It is special method which is called automatically when object is created.
● Name of constructor must be same as class name.
● Constructor does not have return type.
● There are two types of constructor in java:
○ Default constructor
○ Parameterized constructor
Default Constructor
If we do not create any constructor, the Java compiler automatically create a no-arg
constructor during the execution of the program. This constructor is called default
constructor.
Java Compiler will
automatically create a
default constructor if we
do not provide a
constructor in a class
Parameterized Constructor
A Java constructor can also accept one or more parameters. Such constructors are
known as parameterized constructors (constructor with parameters).
we have created a constructor named Main().
Here, the constructor takes a single
parameter. Notice the expression,
Main obj1 = new Main("Java");
Method in Java
❏ A method is a block of code or collection of statements or a set of code grouped
together to perform a certain task or operation.
❏ It is used to achieve the reusability of code. We write a method once and use it
many times.
❏ We do not require to write code again and again.
❏ It also provides the easy modification and readability of code, just by adding
or removing a chunk of code.
❏ The method is executed only when we call or invoke it.
The most important method in Java is the main() method. If you want to read more
about the main() method
Method Declaration
The method declaration provides information about method attributes, such as
visibility, return-type, name, and arguments. It has six components that are known
as method header, as we have shown in the following figure.
Method Signature: Every method has a
method signature. It is a part of the
method declaration. It includes the
method name and parameter list.
Access Specifier
Access specifier or modifier is the access type of the method. It specifies the visibility of the method.
Java provides four types of access specifier:
● Public: The method is accessible by all classes when we use public specifier in our application.
● Private: When we use a private access specifier, the method is accessible only in the classes in
which it is defined.
● Protected: When we use protected access specifier, the method is accessible within the same
package or subclasses in a different package.
● Default: When we do not use any access specifier in the method declaration, Java uses default
access specifier by default. It is visible only from the same package only.
Method: Other Elements
Return Type: Return type is a data type that the method returns. It may have a primitive data
type, object, collection, void, etc. If the method does not return anything, we use void keyword.
Method Name: It is a unique name that is used to define the name of a method. It must be
corresponding to the functionality of the method. Suppose, if we are creating a method for
subtraction of two numbers, the method name must be subtraction(). A method is invoked by its
name.
Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of
parentheses. It contains the data type and variable name. If the method has no parameter, left the
parentheses blank.
Method Body: It is a part of the method declaration. It contains all the actions to be performed. It
is enclosed within the pair of curly braces.
Method:Example
- we have created a method
named square(). The
method takes a number as
its parameter and returns
the square of the number.
Note: If the method does not return any
- Here, we have mentioned value, we use the void keyword as the
return type of the method.
the return type of the
method as int. Hence, the
method should always
return an integer value.
Methods Parmeter
A method parameter is a value accepted by
the method. As mentioned earlier, a method
can also have any number of parameters.
If a method is created with parameters, we
need to pass the corresponding values while
calling the method.
Method Overloading
In Java, two or more methods may have the same name if they differ in parameters
(different number of parameters, different types of parameters, or both).
Why Methodoverloading?
These methods are called overloaded methods and this feature is called method → Suppose, you have to perform the addition of given numbers but
overloading. For example:
there can be any number of arguments (let’s say either 2 or 3 arguments
for simplicity).
In order to accomplish the task, you can create two methods sum2num(int,
int) and sum3num(int, int, int) for two and three parameters respectively.
However, other programmers, as well as you in the future may get
confused as the behavior of both methods are the same but they differ
by name.
The better way to accomplish this task is by overloading methods. And,
Here, the func() method is overloaded. These methods have the same name depending upon the argument passed, one of the overloaded methods is
but accept different arguments. called. This helps to increase the readability of the program.
Note: The return types of the above methods are not the same. It is
because method overloading is not associated with return types.
Overloaded methods may have the same or different return types,
but they must differ in parameters.
How to Perform method Overloading?
There are two ways - 2. by changing the datatype
1. by Changing the of parameters.
parameters.
Method Overloading: Important points
● Two or more methods can have the same name inside the same class if they
accept different arguments. This feature is known as method overloading.
● Method overloading is achieved by either:
○ changing the number of arguments.
○ or changing the data type of arguments.
● It is not method overloading if we only change the return type of methods.
There must be differences in the number of parameters.
Methods: call by value
Call by Value means calling a method with a
parameter as value. Through this, the argument
value is passed to the parameter.
In call by value, the modification done to the
parameter passed does not reflect in the caller's
scope.
Methods:call by reference
Call by Reference means calling a method with a
parameter as a reference. Through this, the
argument reference is passed to the parameter.
the modification done to the parameter passed are
persistent and changes are reflected in the caller's
scope.
this keyword
this keyword is used to refer to the current
object inside a method or a constructor.
we created an object named obj of the class
Main. We then print the reference to the
object obj and this keyword of the class.
Here, we can see that the reference of both
obj and this is the same. It means this is
nothing but the reference to the current
object.
Inner class
In Java, you can define a class within another
class. Such class is known as nested class. For
example, There are two types of nested classes you can create in
Java.
● Non-static nested class (inner class):
a. Member inner class
b. Method:local inner class
c. Anonymours inner class
● Static nested class
Non-static nested class: Member Inner class
A non-static nested class is a class
within another class.
It has access to members of the
enclosing class (outer class). It is
commonly known as inner class.
Since the inner class exists within
the outer class, you must
instantiate the outer class first, in
order to instantiate the inner class.
Instantiating member inner class
Points:
❏ Can access all types of variables (static and non-static) of outer
class.
❏ To instantiate the inner class object -
❏ first instantiate the object of outer class
❏ then only instantiate the object of inner class.
Anonymous inner class
Java anonymous inner class is an inner class without a name
and for which only a single object is created.
An anonymous inner class can be useful when making an
instance of an object with certain "extras" such as overloading
methods of a class or interface, without having to actually
subclass a class.
In simple words, a class that has no name is known as an
anonymous inner class in Java.
It should be used if you have to override a method of class or
interface. Java Anonymous inner class can be created in two
ways:
1. Class (may be abstract or concrete).
2. Interface
Working
1. A class is created, but its name is decided by the compiler,
which extends the Person class and provides the
implementation of the eat() method.
2. An object of the Anonymous class is created that is
referred to by 'p,' a reference variable of Person type.
Method:local inner class
A class i.e., created inside a method, is called
local inner class in java.
Local Inner classes are not a member of any
enclosing classes.
They belong to the block they are defined
within, due to which local inner classes cannot
have any access modifiers associated with
them.
If you want to invoke the methods of the local
inner class, you must instantiate this class
inside the method.
Points
~ End ~
Thank you!!!