Oop Prinyu

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

LESSON 3

Object and Classes


 Java is an Object-Oriented Language. As a language that has the
Object-Oriented feature, Java supports the following fundamental
concepts −  Polymorphism
• Inheritance
• Encapsulation
• Abstraction Classes in Java
• Classes
• Objects  A class is a blueprint from which individual objects are created.
• Instance Example
• Method
• Message Passing
In this chapter, we will look into the concepts - Classes and Objects.
• Object − Objects have states and behaviors. Example: A dog has
states - color, name, breed as well as behaviors – wagging the tail,
barking, eating. An object is an instance of a class.
• Class − A class can be defined as a template/blueprint that describes
the behavior/state that the object of its type support.
Objects in Java

 It is a basic unit of Object Oriented Programming and represents the


real life entities. A typical Java program creates many objects, which
as you know, interact by invoking methods. An object consists of : Creating an Object
1. State : It is represented by attributes of an object. It also reflects the
properties of an object. • As mentioned previously, a class provides the blueprints for objects.
So basically, an object is created from a class. In Java, the new
2. Behavior : It is represented by methods of an object. It also reflects the
keyword is used to create new objects. There are three steps when
response of an object with other objects.
creating an object from a class −
3. Identity : It gives a unique name to an object and enables one object to
interact with other objects. • Declaration − A variable declaration with a variable name with an
Example of an object : dog object type.
• Instantiation − The 'new' keyword is used to create the object.
• Initialization − The 'new' keyword is followed by a call to a Source File Declaration Rules
constructor. This call initializes the new object.
Following is an example of creating an object − As the last part of this section, let's now look into the source file declaration
rules. These rules are essential when declaring classes, import statements
and package statements in a source file.
• There can be only one public class per source file.
• A source file can have multiple non-public classes.
• The public class name should be the name of the source file as well
which should be appended by .java at the end. For example: the class
name is public class Employee{} then the source file should be as
Employee.java.
• If the class is defined inside a package, then the package statement
should be the first statement in the source file.
• If import statements are present, then they must be written between
the package statement and the class declaration. If there are no
If we compile and run the above program, then it will produce the package statements, then the import statement should be the first line
following result − Output: in the source file.
• Import and package statements will imply to all the classes present in
the source file. It is not possible to declare different import and/or
package statements to different classes in the source file.
Accessing Instance Variables and Methods .
Java Package
 Instance variables and methods are accessed via created objects. To
access an instance variable, following is the fully qualified path −  In simple words, it is a way of categorizing the classes and interfaces.
When developing applications in Java, hundreds of classes and
interfaces will be written, therefore categorizing these classes is a
must as well as makes life much easier.
Import Statements
 In Java if a fully qualified name, which includes the package and the
class name is given, then the compiler can easily locate the source
code or classes. Import statement is a way of giving the proper
location for the compiler to find that particular class.
LESSON 4 A constructor is invoked at the time of object or instance creation. For
Example:
Java - Constructors class OOP
• A constructor initializes an object when it is created. It has the same {
name as its class and is syntactically similar to a method. .......
• However, constructors have no explicit return type. // A Constructor
• Typically, you will use a constructor to give initial values to the new OOP() {}
instance variables defined by the class, or to perform any other start- .......
up procedures required to create a fully formed object. }
• All classes have constructors, whether you define one or not, // We can create an object of the above class // using the below
because Java automatically provides a default constructor that statement. This statement // calls above constructor.
initializes all member variables to zero.
OOP obj = new OOP();
• However, once you define your own constructor, the default
constructor is no longer used. Java allows two types of constructors namely −
• If no constructor is defined, the default constructor would initialize
the instance variables to their default values (0 for numeric values, • No argument Constructors
false for boolean type and null for reference types). Syntax: • Parameterized Constructors
No argument Constructors
Following is the syntax of a constructor −
•As the name specifies the no argument constructors of Java does not
accept any parameters instead, using these constructors the instance
variables of a method will be initialized with fixed values for all
objects.
• A constructor that has no parameter is known as default constructor.
If we don’t define a constructor in a class, then compiler creates
When is a Constructor called? default constructor(with no arguments) for the class
Example:
• Each time an object is created using new() keyword at least one
constructor (it could be default constructor) is invoked to assign
initial values to the data members of the same class.
• A constructor that has parameters is known as parameterized
constructor. If we want to initialize fields of the class with your own
values, then use a parameterized constructor.

Example

Parameterized Constructors

• Most often, you will need a constructor that accepts one or more
parameters. Parameters are added to a constructor in the same way
that they are added to a method, just declare them inside the
parentheses after the constructor's name.

You might also like