JAVA:
Java language is based on Object oriented programming.Means in java we will be dealing with Class and Objects.
Principles of Java:
- Objects
- Class
- Encapsulation
- Abstraction
- Polymorphism
- Inheritance
Objects are something which really exists.which has 'presence' and has some 'behavior' is Object.
Ex: Car,Bike,Dog,Pen,Table etc. these all are things which has presence and has behavior.
Class:
Class in java is collection of objects.Class is a logical entity which doesn't exists physically.
In the above figure 'vehicle' is the class and cars,bike,etc are objects of that class.
Encapsulation:
Combining of variables and method in single unit is encapsulation.Class is encapsulated with variables and method.
Ex:
Below code is just a sample code not actual code.To show that class has encapsulated both variables and method.
Class vehicle // this is class
{
String color; //this are variables
int speed;
public void carMethod() // this is method
{
System.out.println("color of car is:"+color);
} // end of method.
} //class closing braces.
Abstraction:
Hiding the unnecessary data from the user is called Abstraction.So to create abstract class we have add 'Abstract' Keyword .
Creating abstract class have some java rules like:
- We have to use abstract keyword before class then this is abstract class.
- Abstract class can have 'ZERO' or Many Abstract Methods.
- We cannot create object to abstract class.
- If we want abstract method in abstract class then we have use abstract keyword before method.
Step 1: Creating abstract class name vehicle.
public abstract class vehicle // this is abstract class
{
abstract void steering(); /*this is abstract method. not implemented.
implementation is done in normal class for this method.*/
public void brake() //this is normal method in java
{
System.out.println("Brake method"); //this is implemenation method
}
}
Step 2:
Creating normal class and inheriting the abstract methods from abstract class by using "Extends" Keyword.
public class Audi extends vehicle
{
void steering() //this method comes from abstract class.
{
System.out.println("Audi steering");/*implementaion is done here for
abstract method of abstract class*/
}
}
Step 3:
Creating class with main method .
public class Driver
{
public static void main(String[] args) //main method
{
Audi a=new Audi();//creating object of audi class
a.steering();//this method comes from audi class.
a.brake(); // this method comes from vehicle class
}
}
Same method exhibits in many forms is known as Polymorphism.
Ex:
Classs A
{
public void sum()
{
System.out.println("This default method");
}
public void sum(int a, int b)
{
System.out.println("Sum is:"+(a+b));
}
public void sum(int a, int b, int c)
{
System.out.println("Sum is :"+(a+b+c));
}
}
In the above example method having same name "sum" but with different arguments.
Inheritance:
Extending properties of one class to another class is inheritance.
Class A
{
public void sum()
{
System.out.println("This is A class method ");
}
}
Class B extends A
{
public void sum()
{
System.out.println("This is B class method "); //implementation differ here.
}
}
No comments:
Post a Comment