Programming |
OOPs is an abbreviation of Object-Oriented Programming System. It’s a programming concept in which the application is developed by the use of Classes and Objects. In OOPs concept, the application is divided into sub-modules depending on the requirements. By the integration of various modules, an application is created. OOPs consists of the following properties:
Advantages of OOPs:
Characteristics of an Object-Oriented Programming:
The process of providing only the functionality and hiding implementation details is called Abstraction. It is also known as data hiding. Some properties or functionalities should be hidden from other users. Abstraction is a logical concept that is implemented by encapsulation.
Program
abstract class Animal {
abstract void run();
}
class Dog extends Animal {
void run() {
System.out.println("Dog running.");
}
public static void main(String args[]) {
Animal animal = new Dog();
animal.run();
}
}
The process of hiding implementation details is known as Encapsulation. It is achieved by marking the instance variables as private and using getter and setters to access those variables.
Program
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The process of using properties of a different class in another class is called as inheritance. The existing class is known as Base/Super Class. The newly defined class is known as Derived/Sub class.
class Animal {
void eat() {
System.out.println("Animals eating generically.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dogs barking.");
}
}
public class InteritanceExample {
public static void main(String args[]) {
Dog dog = new Dog();
dog.bark();
dog.eat();
}
}
The process of performing multiple tasks with the single name is called as Polymorphism. By the help of inheritance we can provide multiple implementations of same methods. Overloading and overriding help us achieve polymorphism in Java.
Program
class Animal {
public void sound() {
System.out.println("General sound of animals.");
}
}
class Bear extends Animal {
@Override
public void sound() {
System.out.println("Bear growling.");
}
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dogs barking.");
}
}
A class is a blueprint of an Object. It can be treated as custom or user defined type. It provides a structure for the object. Class is the place where properties and functionalities of the object are placed. Any number of objects can be created in a class.
Syntax
[<modifier>] class <classname> [extends<classname>] [implements <interfacename(s)>] {
Members of class
}
Program
public class ClassExample {
int x;
}
Object is known as an instance of a class. An object is an entity that has a state and behavior.
Program
public class ObjectExample {
int x = 5;
public static void main(String[] args) {
ObjectExample objectExample = new ObjectExample();
System.out.println(objectExample.x);
}
}