Class :
Class is user defined data type. Class is a logical representation of an Object. That means Class is used to define an object, how you want to create your software object.
For Example : An Engineer will design a car to build a real object CAR. Same like that we are designing a class to create software objects.
After designing how many objects you want to create, using the same design you can create. All those objects are not same.
Syntax : Example :
class classname class Car
{ {
data fields; int wheels;
blocks; {
wheels =4;
}
methods; void driving() {
statements;
}
} }
Object :
Object is a physical representation of a class. Every object will contain 3 things.
1. State : Data fields of an object.
2. Methods : Behavior of an object.
3. Identity : By name and memory address.
Syntax : Example :
// This is the Actual Object Creation
new constructor; new Car();
// This is for giving some reference name to object
classname referencename = new constructor; Car cr = new Car();
Abstraction :
Abstraction means " Minimizing inter-dependencies among separately written modules by defining strict external interfaces ". It is a methodology, By using Encapsulation, Abstract classes and interfaces we can achieve this Abstraction. That means by using Encapsulation concept we can hide the data and by using Abstract classes and interfaces we can hide the implementation so that we can call Abstraction is nothing but hiding.
Encapsulation :
Encapsulation means wrapping/binding/packing of data and methods, operating on that data into a single component. So that we can restrict the data instead of preventing.
That means by using getters and setters we can restrict the data by giving permissions to outside world.For which data you want to update/write to that data only provide setters otherwise if you want to give permission to read only then to that data provide only getters.
Example :
class Age
{
private int age;
private Date dob;
public void setDateOfBirth(Date dob)
{
this.dob=dob;
age = // Logic to calculate Age
}
public int getAge()
{
return age;
}
}
In this example we are calculating age based on the DOB so we can't give age as input type so I did not write setter method for age and I don't want to expose exact date of birth to outside world so I did not provide getter for Date of Birth.
Polymorphism :
Poly means Many and Morphism means Forms. So Polymorphism means one thing can act as a many forms based on where and how its exists.
There are 2 types of polymorphism
1. Compile-time
2. Run-time
The best example for polymorphism is ourself. we are only one person will behave like many persons based on where you exists with whom you exists like in class, theater, with friends, family, friends etc.
So that At object level methods indicating the behaviour, so to achieve polymorphism we use
1. Method overloading
2. Method Overriding
Inheritance :
Inheritance is a concept of inheriting the properties and methods of one class into new class for the purpose of reusing the code. When the class need to have parent child relation ship we need to use inheritance concept. That means all the parents properties will not get to child and the child can add its own properties at this situation we can use inheritence instead of directly creating the objects.
Java does not support multiple inheritance at class level.
Syntax :
class Parent
{
//statements
}
class Child extends Parent
{
//statements;
}
"extends" is the keyword to build parent child relation ship.