Pillars Of Object Oriented Programming
There are 4 pillars of OOP
- Encapsulation
- Abstraction
- Polymorphism
- Inheritance
General Overview of Class & Objects
The class is considered a blueprint for the object. Example: building layout for the building Object
Objects are physical Objects. Or when we actually allocate memory to it.
Note: Every class will consist of two things (not compulsary to have both)
- Behaviour/ activity--- called as method
- To store data --- called as variables
class HelloWorld {
public static void main(String[] args) {
classes object=new classes();
System.out.println(object.a);
}
}
class classes{
int a=20;
}
We use . to access the variables inside the class
Private Variables
- In the above example we can access the variables of the class outside of the class, which can be a security issue
- To avoid this we can use private keyword
examples:
If we try to access it from outside we will get an error
The solution we use to methods getter and setter
example:
class HelloWorld {
public static void main(String[] args) {
classes object=new classes();
object.setter(20);
System.out.println(object.getter());
}
}
class classes{
private int a;
public int getter(){
return a;
}
public void setter(int x){
a=x;
}
}
In this way we achieve encapsulation.
Encapsulation
- In object-oriented computer programming (OOP) languages, the notion of encapsulation (or OOP Encapsulation) refers to the bundling of data, along with the methods that operate on that data, into a single unit.
- Containers are just one example of encapsulation in coding where data and methods are bundled together into a single package.
- A key benefits to hiding information about attributes and methods using encapsulation in programming is that it prevents other developers from writing scripts or APIs that use your code.
Shasowing Problem
class HelloWorld {
public static void main(String[] args) {
classes object=new classes();
object.setter(20);
System.out.println(object.getter());
}
}
class classes{
private int a;
public int getter(){
return a;
}
public void setter(int x){
a=a;//here jvm will get confused if it is local variable or it is instance variable
}
}
The solution is to use this keyword The other way of making a setter is called a constructor
Constructor
- Constructor is a specialized setter method. It is called the moment we create an Object Some Important Behaviour Of Constructors
- The first line of the constructor always has a call to super()
- If no constructor is provided by the programmer then JVM will insert a default constructor
- The default constructor needs to be non-parameterized
- If we make a parameterized call to the default constructor we will get a compile time error
- Default constructor is inserted by JVM only if we do not have any constructor given be the programmer
- There is no return value of a constructor
example
- Constructor By Programmer example:
class HelloWorld {
public static void main(String[] args) {
classes object=new classes(20);
System.out.println(object.getter());
}
}
class classes{
private int a;
classes(int number){
this.a=number;
}
public int getter(){
return this.a;
}
}
- Default Constructor
class HelloWorld {
public static void main(String[] args) {
classes object=new classes();
System.out.println(object.getter());
}
}
class classes{
private int a;
public int getter(){
return this.a;
}
}
Note: this() is used to call the class constructor and super() is used to call the parent constructor.Both this() and super() cannot exist together