Object Oriented Programming in Java
Object Oriented Programming in Java
Programming
Java
Java - What is OOP?
object-oriented programming is about creating objects that
contain both data and methods.
Object-oriented programming has several advantages over
procedural programming:
Example
Java Inheritance Example
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer
salary is:"+p.salary);
System.out.println("Bonus of
Programmer is:"+p.bonus);
} Programmer is the subclass/child
} class and Employee is the super
class/parent class. The relationship
between the two classes
is Programmer IS-A Employee.
Polymorphism
Polymorphism is the ability of an object to take on different forms. In
Java, polymorphism refers to the ability of a class to provide different
implementations of a method, depending on the type of object that is
passed to the method.
Abstraction
is a process of hiding the implementation details and showing only
functionality to the user.
Another way, it shows only essential things to the user and hides the
internal details, for example, sending SMS where you type the text
and send the message. You don't know the internal processing about
the message delivery.
Encapsulation in Java
is a process of wrapping code and data together into a single unit,
for example, a capsule which is mixed of several medicines.
Private: The access level of a private modifier is only within the class. It
cannot be accessed from outside the class.
Default: The access level of a default modifier is only within the
package. It cannot be accessed from outside the package. If you do not
specify any access level, it will be the default.
Protected: The access level of a protected modifier is within the
package and outside the package through child class. If you do not
make the child class, it cannot be accessed from outside the package.
Public: The access level of a public modifier is everywhere. It can be
accessed from within the class, outside the class, within the package
and outside the package.
Java Constructors