Java Chapter 3
Java Chapter 3
Java classes
Java objects
Java Methods
Command Line Arguments
Constructors
this Keyword
super Keyword
static Keyword
final Keyword
Finally
Java Comments
Java Classes
A Java class is the template from which objects are created. It is a blueprint for an object. A class
is created using the keyword class.
A class in Java is a set of objects which shares common characteristics/ behavior and common
properties/ attributes.
It is a user-defined blueprint or prototype from which objects are created. For example, Student is
a class while a particular student named Ravi is an object.
Properties of Java Classes:-
Data memberClass is not a real-world entity. It is just a template or blueprint or prototype from
which objects are created.
Class does not occupy memory.
Class is a group of variables of different data types and a group of methods.
A Class in Java can contain:
Method
Constructor
Nested Class
Interface
Java Classes
Class Declaration in Java:-
access_modifier class <class_name>
{
data member;
method;
constructor;
nested class;
interface; }
In general, class declarations can include these components, in order:
Modifiers: A class can be public or has default access.
Class keyword: class keyword is used to create a class.
Class name: The name should begin with an initial letter (capitalized by convention).
Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword
extends. A class can only extend (subclass) one parent.
Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded
by the keyword implements. A class can implement more than one interface.
Body: The class body is surrounded by braces, { }.
Java Objects
An object in Java is a basic unit of Object-Oriented Programming and represents real-life.
Entities
Objects are the instances of a class that are created to use the attributes and methods of a
class.
A typical Java program creates many objects, which as you know, interact by invoking
methods. An object consists of :
State: It is represented by attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by the methods of an object. It also reflects the response of an
object with other objects.
Identity: It gives a unique name to an object and enables one object to interact with other
objects.
Example of an object: dog
1. Predefined Method:- In Java, predefined methods are the method that is already defined in the Java
class libraries is known as predefined methods. It is also known as the standard library method or built-
in method. We can directly use these methods just by calling them in the program at any point.
2. User-defined Method:- The method written by the user or programmer is known as a user-defined
method. These methods are modified according to the requirement.
Execution Steps –
javac Test.java
java Test 10 20
Output – sum = 30
Constructor
s
In java, Constructor is a special type of method. It is called at the time of object creation. Memory of
the object is allocated at the time calling constructor.
Every class must have a constructor. If there is no constructor in class then java compiler automatically
creates a default constructor.
s1.display();
s2.display();
}
}
Output – 0 null
0 null
Constructor
s
In java, there are three types of constructor:
Parameterized Constructor
Non – Parametrized Constructor
Default Constructor
A constructor has a specific parameter is called Parameterized Constructor. A constructor with no parameter is
called Non-Parameterized Constructor.
Constructor
Parameterized Constructor s Non-Parameterized Constructor
A constructor has a specific parameter is called Parameterized Constructor. A constructor with no parameter is
called Non-Parameterized Constructor.
Default Constructor
Constructor generated by compiler if there is no constructor in program, that constructor is known as Default
Constructor.
Constructor
Constructor Overloading
s
Constructor Overloading means that a class having more than one constructor with different parameter.
void show(){
Constructor Overloading
System.out.println("Id = "+id+"\t"+"Name = "+name);
class Demo{
}
int id, salary;
void show1(){
String name;
System.out.println("Id = "+id+"\t"+"Name = "+name+"\tSalary = "+salary);
Demo(int i,
String n){ }
public static void main(String args[]){
id = i;
Demo t=new Demo(101, "Manish");
name = n;
} Demo t1=new Demo(102, "Manish",
2000);
Demo(int i, String n, int sal){
t.show(); t1.show1();
id = i;
}}
name = n; salary = sal;
Output – Id = 101 Name =
}
Manish
Id = 102 Name = Manish Salary
= 2000
Constructor
Constructor Vs. Method
s
Java Constructor Java Method
A constructor is used to initialize the A method is used to expose the
state of an object. behavior of an object.
The constructor name must be same The method name may or may not be same
as the class name. as the class name.
this
Keywords
In java, this is a reference variable that refers to the current class object.
this keyword is used to differentiate local and instance variable when both the variable name is same.
Static Block - In java static block is used to initialize static data member and it is executed before the
method at the time of class loading.
main class Demo{
static {
System.out.println("Static Block");
}
public static void main(String args[]) { Output - Static Block
System.out.println("Main Method"); Main Method
}
}
static
Static Variable
keyword
A variable declare with static keyword is called Static Variable.
After declaration static variable, a single copy of the variable is created and divided among all objects at the
class level.
Static variable can be created at class-level only and it gets memory only once in the class area at the time of
class loading.
Static variable can’t re-initialized.
Variable - If you declare a variable with final keyword then it must be initialized.
Final Variable Final Variable (Can’t change value)
class Demo class Demo
{ final int x = { final int x =
10; void show() 10; void show()
{ { x = 20;
System.out.prin System.out.prin
tln(x); tln(x);
} }
public static void main(String args[]) { public static void main(String args[]) {
Demo d = new Demo(); Demo d = new Demo();
d.show(); d.show();
}} }}
Output - 10 Output - error: cannot assign a value
to final variable x
final
Method - If you declare a method as final itkeyword
means that you cannot override (Discuss in Inheritance) this
method.
Final Method Cannot Override
class Test {
final void show()
{ System.out.println("Parent Class
Method");
}
}
class Demo extends Test{
void show(){
System.out.println("Cannot override due to final method.");}
public static void main(String args[]) {
Demo d = new Demo();
d.show();
}
}
finally {
// Statements;
}
Java
The Java comments are the statements inComments
a program that are not executed by the compiler and
interpreter.
Comments are used to make the program more readable by adding the details of the code.
It makes easy to maintain the code and to find the errors easily.
The comments can be used to provide information or explanation about the variable, method, class,
or any statement.
It can also be used to prevent the execution of program code while testing the alternative code.
Documentation comments are usually used to write large programs for a project or software application as it
helps to create documentation API. These APIs are needed for reference, i.e., which classes, methods,
arguments, etc., are used in the code.
To create documentation API, we need to use the javadoc tool. The documentation comments are placed
between /** and */.