Module 2 - Java Programming
Module 2 - Java Programming
• Class Fundamentals
• Declaring objects and assigning object reference variables
array of objects
• constructors .
• Methods
• overloading methods
• constructors
• this keyword
• static block
• nested class
• inner class
• garbage collection.
• finalize()- Inheritance – types - use of super - Polymorphism – abstract class –
interfaces – packages and sub packages.
Class Fundamentals
• A class can be defined as a template that describes
the functionalities /attributes that the instance of
this class could support
• Classes tell the compiler how to build a certain
object during runtime.
• Object – The object which is an instance of a class
have both functionalities and attributes.
– Example: A car has attributes like colour, number of
wheel, model ,price and functionalities like forward,
reverse, switch on indicators, cleaning windshield etc.,
Basic Structure of a Class
class <name> {
fields ;
methods;
}
Classes and Objects (Examples)
5
Object and Object Reference
• Object is an instance of a class and class is a
template; here Car and Dog are classes shown
below
6
Creating Objects
Rectangle rect1;//Declare the object
Rect1=new Rectangle();//instantiate the object
8
Arrays of Objects
9
Syntax for Array of Object Creation
Classname identifier[] = new Classname [size];
where size is the number of array objects
required
Output
Output
Constructors
• In Java, a constructor is a block of codes
similar to the method.
• It is called when an instance of the class is
created. At the time of calling constructor,
memory for the object is allocated in the
memory.
• It is a special type of method which is used to
initialize the object.
• Every time an object is created using the
new() keyword, at least one constructor is
called.
Rules for creating constructor :
• Constructor name must be the same as its
class name
• A Constructor must have no explicit return
type
Constructor Types :
• Default constructor
• Parameterized constructor.
Default Constructor
• A constructor is called "Default Constructor"
when it doesn’t have any parameter
Purpose of Default Constructor
It used to provide the default values to the
object like 0, null.
Another Example
Parameterized Constructor
• A constructor which has a specific number of
parameters.
• It is used to provide different values.
Method Overloading
• Multiple methods in a class, can have the
same name with different parameters.
• To perform only one operation, having same
name of the methods increases the readability
of the program
Methods in Java
• Methods are functionalities defined to operate on class
data members
• In the above example of student class show() is a
method which displays the data member of student
class
• A method can be static or non static
• Static method can be invoked without an object of that
class.
• Non static method should be invoked by an object of
that class.
Static and non static
Constructor Overloading
• In Java, a constructor is just like a method but
without return type.
• It can also be overloaded like Java methods.
• It 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.
Example2
Java Constructor vs Java Method
• A constructor must not have a return type.
• A method must have a return type.
• The constructor is invoked implicitly
• The method is invoked explicitly.
• The constructor name must be same as the
class name.
• The method name may or may not
be same as the class name
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.
• 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.
Example-1. (Problem without this)
Example-2. (Solution)
Example-3.(Not required this)
Java Modifiers
• Modifiers are keywords used to change their meanings.
Java has a wide variety of modifiers, including the
following:
• Access Modifiers : to set access levels for classes,
variables, methods and constructors. The four access
levels are :
• default : Visible to the package
• private : Visible to the class only
• public : Visible to the world.
• protected : Visible to the package and all subclasses.
Non Access Modifiers
• static : used for creating class methods and
variables.
• final : used for finalizing the implementations
of classes, methods, variables.
• abstract : used for creating abstract classes
and methods.
• synchronized and volatile modifiers, which
are used for threads
private Access Modifier
private Access Modifier
default Access Modifier
interface Showable
{
void show();
}
class A7 implements Printable,Showable{
public void print()
{
System.out.println("Hello");
}
public void show()
{System.out.println("Welcome");
}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
• A class can implement more than one
interface.
• Multiple inheritance can be implemented by
interface.
• Interface can be inherited another interface
• Also implement the methods from parent
interface if you are implement sub interface
Packages
• Int j=6;
• Int j=iobj.intvalue();un wrapping
Autoboxing in Wrapper Class
• Unboxing in Wrapper Class
• Unboxing is used to convert the Wrapper
class object into corresponding primitive data
types.
Enhanced For Loop
• Syntax