//Invoke an instance method that operates on the object's value
char c = s.chartAt(4);
```
> INSTANCE VARIABLES
```java
public class Charge {
//Instance variable declarations
private final double rx, ry;
private final double q;
}
```
### METHODS
```java
public static double sum (int a, int b) { //double is the return type, sum is the method's name, a and b are two arguments of type int;
int result; //local variable
result = a + b;
return result;//return statement;
}
```
### CLASS DECLARATION
```java
class MyClass {
// field, constructor, and
// method declarations
}
```
**Example:**
```java
public class Bicycle {
// the Bicycle class has
// three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has
// one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has
// four methods
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
```
>DECLARING CLASSESS IMPLEMENTATING AN INTERFACE AND EXTENDING PARENT CLASS
```java
class MyClass extends MySuperClass implements YourInterface {
// field, constructor, and
// method declarations
}
```
* MyClass is a subclass of MySuperClass and that it implements the YourInterface interface.
> CONSTRUCTORS
* A class contains constructors that are invoked to create objects from the class blueprint.
* Constructor declarations look like method declarations—except that they use the name of the class and have no return type
* Each and every class has defualt No-args constructor.
```java
public class Bicycle{
private int gear;
private int cadence;
private int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) { //args-constructor
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
public Bicycle(){//No-args constructor
super();
}
}
```
### POLYMORPHISM
* Polymorphism is the concept where an object behaves differently in different situations.
* There are two types of polymorphism
1. compile time polymorphism
2. runtime polymorphism.
#### 1. Compile Time Polymorphism
* Compile-time polymorphism is achieved by method overloading.
* method overloading is creating multiple method with methods name is same and arguments are different.
```java
public class Circle {
public void draw(){
System.out.println("Drwaing circle with default color Black and diameter 1 cm.");
}
public void draw(int diameter){ //method draw() overloaded.
System.out.println("Drwaing circle with default color Black and diameter"+diameter+" cm.");
}
public void draw(int diameter, String color){ //method draw() overloaded.
System.out.println("Drwaing circle with color"+color+" and diameter"+diameter+" cm.");
}
}
```
#### 2. Run Time Polymorphism
* Run-time polymorphism is achieved by method overriding.
* Runtime polymorphism is implemented when we have an **“IS-A”** relationship between objects.
* method overriding is the subclass has to override the superclass method.
```java
public interface Shape {
public void draw();
}
```
```java
public class Circle implements Shape{
@Override
public void draw(){
System.out.println("Drwaing circle");
}
}
```
```java
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Drawing Square");
}
}
```
*`Shape` is the superclass and there are two subclasses `Circle` and `Square`
* Below is an example of runtime polymorphism.
```java
Shape sh = new Circle();
sh.draw();
Shape sh1 = getShape(); //some third party logic to determine shape
sh1.draw();
```
### INHERITANCE
* Inheritance is the mechanism of code reuse.
* The object that is getting inherited is called the superclass and the object that inherits the superclass is called a subclass.
* We use `extends` keyword in java to implement inheritance from class.
* We use `implements` keyword in java to implement inheritance from interface.
```java
public class Superclass{
// methods and fields
}
```
```java
public interface Superinterface{
// methods and fields
}
```
```java
public class Subclass extends Superclass implements Superinterface{
// methods and fields
}
```
### Abstraction
* Abstraction is the concept of hiding the internal details and describing things in simple terms.
* Abstraction can be achieved by two ways.
1. Abstract Class
2. Interface
#### 1. Abstract Class
* An abstract class must be declared with an `abstract` keyword.
* It can have abstract and non-abstract methods.
* It cannot be instantiated.
* It can have constructors and static methods also.
* It can have final methods which will force the subclass not to change the body of the method.
```java
abstract class Flower{
abstract String Smell(); //abstract method.
String Oil(){ // non-abstract method.
System.out.println("Flower Oil is good.");
}
}
public class Lily extends Flower{
private String Smell(){ // implementation of abstarct method.
System.out.println("Lily smell's lovender.");
}
}
```
#### 2. Interface
* Interface is a blueprint of a **class**.
* It can have only abstract methods. [Except Java 8 and next versions.]
* Since Java 8, we can have **default and static** methods in an interface.
```java
interface print{
void printPaper();
}
public class A4 implements print{
public void printPaper(){
System.out.println("A4 Page Printed. ");
}
}
```
### Encapsulation
* Encapsulation is used for access restriction to class members and methods.
* Encapsulation is the technique used to implement abstraction in OOP.
* As in encapsulation, the data in a class is hidden from other classes, so it is also known as **data-hiding**.
* Encapsulation can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.
* Best example of Encapsulation is POJO (Plain-Java-Object-Class).
```java
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
## ADVANCE DATA TYPE
* **STACK DATA TYPE**
```java
public class Stack<Item> implements Iterable <Item>
Stack() //create an empty stack
boolean isEmpty() //return if the stack empty
void push(Item item) // push an item onto the stack
Item pop() //return and remove the item that was inserted most recently
int size() //number of item on stack
```
* **QUEUE DATA TYPE**
```java
public class Queue<Item> implements Iterable<Item>