JAVA NOTES
Everything in Java is associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car has attributes, such as
weight and color, and methods, such as drive and brake.
ABSTRACT
Functions - write an algorithm once to be used in many situations
Objects - group of related set of attributes and behaviors into a class
Framework & API - large groups of objects that support a complex activity
Framework can be used “as is” or be modified to extend the basic behavior.
CLASS
- Most fundamental aspects of OOP.
- Template or prototype that defines a type of object.
- A blueprint from which an object is actually made.
- Describes the state or the data that each object contains
- Describes the behavior that each object exhibits
- Classes in Java support three key features of OOP
1. ENCAPSULATION
- Binding the data with the code that manipulates it.
- It keeps the data and the code safe from external interference.
The idea of encapsulation is to keep classes separated and prevent them from having
tightly coupled with each other.
2. INHERITANCE
- Inheritance is the mechanism by which an object acquires the some/all
properties of another object.
- It supports the concept of hierarchical classification.
JAVA NOTES
3. POLYMORPHISM
- Polymorphism means to process objects differently based on their data
type.
- In other words it means, one method with multiple implementations, for a
certain class of action. And which implementation to be used is decided at
runtime depending upon the situation (i.e., data type of the object)
- This can be implemented by designing a generic interface, which provides
generic methods for a certain class of action and there can be multiple
classes, which provides the implementation of these generic methods.
Polymorphism could be static and dynamic both. Method Overloading is static
polymorphism while Method overriding is dynamic polymorphism.
public class MyPoint { In MyPoint class, x and y are called data
private int x, y; // attributes members, instance variables or attributes
public void setX (int n) {
x = n;
}
public void setY (int n) { y = n; }
public int getX () {
return x;
}
public int getY () { return y; }
public void display () {
System.out.println(“(“ + x + “,” + y “)”);
JAVA NOTES
OBJECT
- Instance of a class
- In order to create an instance from a class, use the new keyword
MyPoint p = new MyPoint();
P is a reference variable
INSTANCE VARIABLES OR ATTRIBUTES
- Define the state of an object in a class
- Can also be objects
METHODS
- Function that provide processing to the object’s data
- Determine the behavior of an object
- The collection of all publicly available methods is called the class interference
ACCESSING OBJECT INTERFERENCE
- The dot notation: <object>.<member>
- To access the member of the class, use the dot operator
p.setX(20);
p.display();
- The dot operation is used to access object members including attributes and
methods
Examples:
objl.setX(47);
Objl.x = 47; - This is only allowed if x is public
Constructors
A constructor is a method that is automatically invoked when an object is created. It is
normally used to initialize data members. It has the same name as the name of the
class, can receive parameters but has no return value.
JAVA NOTES
Example:
public class MyPoint {
private int x;
private int y;
// Constructor
public MyPoint(int a, int b) {
x = a;
y = b;
}
}
Default Constructor
- If the programmer does not provide any constructor, a default no-argument
constructor is automatically provided:
- The default constructor takes no arguments.
- The default constructor has no body.
- This enables the user to create object instances with `new ClassName()`
without having to write a constructor.
- If the programmer writes a constructor, the default constructor is no longer
provided.
Constructing and Initializing Objects
Calling new ...() causes the following events to occur:
- Memory is allocated for the new object, and instance variables are initialized to
their default values.
- Explicit attribute initialization is performed.
- The appropriate constructor is executed.
JAVA NOTES
Declaring
MyPoint p;
- Allocates space only for the reference. No object is created unless the `new`
operator is invoked.
Example:
MyPoint p = new MyPoint(10, 20);
MyPoint q = p;
q.setX(50);
p.display(); // displays (50, 20)
The this Reference
In the `MyPoint` class, in the method display(), which x and y values are displayed?
They are the `x` and `y` values of the object that invoked `display()`.
Example:
MyPoint p = new MyPoint(20, 20);
p.display();
- The x and y values of the object referenced by p are displayed.
How does the display know it is the x and y values of the object referenced by p that
should be displayed?*
- When a method is invoked by an object, the object reference is implicitly
transmitted as a hidden first argument to the method. Inside the method, the
reference has a name, and it is called this.
Class Definition Example
public class MyPoint {
private int x;
private int y;
public MyPoint(int x, int y) {
JAVA NOTES
this.x = x;
this.y = y;
}
}
Parameter Passing in Java
- Java only passes parameters by value.
- When an object is passed as a parameter to a method, only the reference is
transmitted.
JAVA NOTES