We learn about Object Oriented Programming principles:
- Classes vs Objects
- Abstraction
- Instantiation
- Encapsualtion with Setters and Getters
- Inheritance via Parent and Child classes
- Polymorphism
Object-Oriented Programming (OOP) is a programming paradigm that uses the concept of “objects” to organize and structure code. Here are some key concepts in OOP explained in a simple way:
1. Class:
– A class is like a blueprint or a template for creating objects.
– It defines the properties (attributes) and behaviors (methods) that the objects created from the class will have.
2. Object:
– An object is an instance of a class. It is a concrete, tangible entity that has specific attributes and can perform certain actions.
– For example, if “Car” is a class, an object could be a specific car with its unique characteristics.
3. Encapsulation:
– Encapsulation is the bundling of data (attributes) and the methods that operate on that data into a single unit (class).
– It helps in hiding the internal details of how an object works and exposing only what is necessary.
4. Inheritance:
– Inheritance is the ability of a class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class).
– It promotes code reuse and the creation of a hierarchy of classes.
5. Polymorphism:
– Polymorphism allows objects of different classes to be treated as objects of a common base class.
– It enables flexibility and extensibility in the code by allowing objects to be used interchangeably.
6. Abstraction:
– Abstraction is the process of simplifying complex systems by modeling classes based on their essential properties.
– It involves focusing on the relevant details of an object while ignoring unnecessary details.
Here’s a simple analogy:
– Class: Think of a class as a blueprint for a car.
– Object: An object is a specific car built from that blueprint.
– Encapsulation: The car’s engine, wheels, and other components are encapsulated within the car.
– Inheritance: Different types of cars (sedan, SUV) inherit common features from a generic “Car” class.
– Polymorphism: You can refuel any type of car using the same gas station, treating them as objects of a common class.
– Abstraction: When you drive a car, you don’t need to understand all the internal workings; you interact with abstract concepts like the steering wheel and pedals.
Creating an object from a class
also known as creating an instance or instantiation.
public class Person {
int age; //property
//method
public void run(String name) {
System.out.println(name + " is " + age + " years old.");
System.out.println(name + " is running now...");
}
}
/**
* Instantiation = Making an object from a class
* @author azmeer
*/
public class W08aOOP1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("OOP test");
//creating an object form Person class
Person sunil = new Person();
sunil.age = 20;
sunil.run("Sunil");
//creating another object
Person nimal = new Person();
nimal.run("Nimal");
}
}
Protecting class properties ‘encapsulation’
We need setter to set values and getters to get the values of private properties.
/**
* Class with sett/get methods
* @author azmeer
*/
public class Person {
private int age; //property
//method
public void run(String name) {
System.out.println(name + " is " + getAge() + " years old.");
System.out.println(name + " is running now...");
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int a) {
if (a < 1 || a > 120) {
System.out.println("Age should be between 1 and 120");
} else {
this.age = a;
}
}
}
/**
* Encapsulation example
* @author azmeer
*/
public class W10aSetget {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("OOP Setter/Getter example");
//creating an object form Person class
Person sunil = new Person();
sunil.setAge(1);
sunil.run("Sunil");
System.out.println("age = " + sunil.getAge());
}
}
Class constructor method
This method is called whenever we create an object out of a class. We can pass parameters at time of object creation.
/**
* Class with constructor method
* @author azmeer
*/
public class Person {
private int age; //property
//constructor method
Person(int i) {
this.age = i;
}
//method
public void run(String name) {
System.out.println(name + " is " + getAge() + " years old.");
System.out.println(name + " is running now...");
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int a) {
if (a < 1 || a > 120) {
System.out.println("Age should be between 1 and 120");
} else {
this.age = a;
}
}
}
/**
* Constructor example
* @author azmeer
*/
public class W10bConstructor {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("OOP Constructor example");
//creating an object form Person class
Person sunil = new Person(20);
sunil.run("Sunil");
System.out.println("age = " + sunil.getAge());
}
}
Parent class ‘inheriting’ child classes
The child class will have all properties and methods of the parent class. But this can be controlled with ‘private’, ‘public’ and ‘protected’ access modifiers. In this example, we create ‘Staff’ and ‘Student’ child classes from ‘Person’ parent class.
/**
* Parent class
* @author azmeer
*/
public class Person {
private int age; //property
//constructor method
Person(int i) {
this.age = i;
}
//method
public void run(String name) {
System.out.println(name + " is " + getAge() + " years old.");
System.out.println(name + " is running now... (PARENT)");
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int a) {
if (a < 1 || a > 120) {
System.out.println("Age should be between 1 and 120");
} else {
this.age = a;
}
}
}
We ‘overide’ the run() method in Student class. This means, it replaces the run method of the ‘Person’ parent class with a new one.
/**
* Student child class of parent class Person
* Student is inheriting Person
* @author azmeer
*/
public class Student extends Person {
public Student(int i) {
super(i);
}
//method
//polymorphism
@Override
public void run(String name) {
System.out.println(name + " is " + getAge() + " years old.");
System.out.println(name + " is running faster now... (STUDENT)");
}
}
/**
* Staff child class of parent class Person
* Staff is inheriting Person
* @author azmeer
*/
public class Staff extends Person {
public Staff(int i) {
super(i);
}
}
/**
* Inheritance example
* @author azmeer
*/
public class W10cChildClass {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("OOP inheritance example");
Student malini = new Student(25);
System.out.println("age = " + malini.getAge());
malini.run("Malini");
Staff srimal = new Staff(30);
System.out.println("age = " + srimal.getAge());
srimal.run("Srimal");
}
}