The document discusses key concepts of object-oriented programming (OOP) including objects, classes, constructors, encapsulation, inheritance, and polymorphism. It provides examples to illustrate each concept. Objects contain data (states) and behaviors (methods). A class acts as a blueprint to create objects. Constructors initialize objects. Encapsulation hides implementation details and controls access via getters and setters. Inheritance allows classes to acquire properties and behaviors of other classes. Polymorphism allows the same method to process objects differently based on their data type.
5. Object-oriented programming System(OOPs) is a programming
paradigm based on the concept of “objects” that contain data and
methods. The primary purpose of object-oriented programming is to
increase the flexibility and maintainability of programs. Object
oriented programming brings together data and its
behaviour(methods) in a single location(object) makes it easier to
understand how a program works. We will cover each and every
feature of OOPs in detail so that you won’t face any difficultly
understanding OOPs Concepts.
OOPs Concepts – Table of Contents
What is an Object
What is a class
Constructor in Java
Object Oriented Programming Features
Abstraction
Encapsulation
Inheritance
6. What is an Object
Object: is a bundle of data and its behavior(often known as
methods).
Objects have two characteristics: They
have states and behaviors.
Examples of states and behaviors
Example 1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
7. class House {
String address;
String color;
double are;
void openDoor() {
//Write code here }
void closeDoor()
{ //Write code here } ...
}
method
object properties
8. Example 2 :
Let’s take another example.
Object: Car
State: Color, Brand, Weight, Model
Behavior: Break, Accelerate, Slow Down, Gear
change.
the states and behaviors of an object, can be
represented by variables and methods in the
class respectively.
9. What is a Class in OOPs Concepts
A class can be considered as a blueprint using which you can create as many objects as you like.
For example, here we have a class Website that has two data members (also known as fields,
instance variables and object states). This is just a blueprint, it does not represent any website,
however using this we can create Website objects (or instances) that represents the websites. We
have created two objects, while creating objects we provided separate properties to the objects using
constructor.
public class Website {
String webName;
int webAge;
Website(String name, int age)
{ this.webName = name;
this.webAge = age; }
public static void main(String args[]){
//Creating objects
Website obj1 = new Website(“ilias.com", 5);
Website obj2 = new Website("google.com", 18);
//Accessing object data through reference
System.out.println(obj1.webName+" "+obj1.webAge);
System.out.println(obj2.webName+" "+obj2.webAge); } }
10. What is a Constructor
Constructor looks like a method but it is in fact not a
method. It’s name is same as class name and it does not
return any value. You must have seen this statement in
almost all the programs I have shared above:
MyClass obj = new MyClass();
If you look at the right side of this statement, we are calling
the default constructor of class myClassto create a new
object (or instance).
11. Example of constructor
public class ConstructorExample {
int age;
String name; //Default constructor
ConstructorExample()
{ this.name=“ilias";
this.age=40; } //Parameterized constructor
ConstructorExample(String n,int a)
{ this.name=n; this.age=a; }
public static void main(String args[]){
ConstructorExample obj1 = new ConstructorExample();
ConstructorExample obj2 = new ConstructorExample("Saiful", 26);
System.out.println(obj1.name+" "+obj1.age);
System.out.println(obj2.name+" "+obj2.age); } }
13. Abstraction
One of the most fundamental concept of OOPs is Abstraction. Abstraction
is a process where you show only “relevant” data and “hide” unnecessary
details of an object from the user. For example, when you login to your
Amazon account online, you enter your user_id and password and press
login, what happens when you press login, how the input data sent to
amazon server, how it gets verified is all abstracted away from the you.
Another example of abstraction: A car in itself is a well-defined object,
which is composed of several other smaller objects like a gearing system,
steering mechanism, engine, which are again have their own subsystems.
But for humans car is a one single object, which can be managed by the
help of its subsystems, even if their inner details are unknown.
14. public abstract class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number; }
public double computePay() {
System.out.println("Inside Employee computePay");
return 0.0; }
public void mailCheck() { System.out.println("Mailing a check to " + this.name + " " +
this.address); }
public String toString() { return name + " " + address + " " + number; } public String
getName() { return name; } public String getAddress() { return address; } public void
setAddress(String newAddress) { address = newAddress; }
public int getNumber() { return number; } }
15. What is encapsulation?
The whole idea behind encapsulation is to hide the implementation details
from users. If a data member is private it means it can only be accessed
within the same class. No outside class can access private data member
(variable) of other class.
However if we setup public getter and setter methods to update (for
example void setSSN(int ssn))and read (for example int getSSN()) the
private data fields then the outside class can access those private data
fields via public methods.
16. class EncapsulationDemo{
private int ssn;
private String empName;
private int empAge;
//Getter and Setter methods
public int getEmpSSN(){ return ssn; }
public String getEmpName(){ return empName; }
public int getEmpAge(){ return empAge; }
public void setEmpAge(int newValue){ empAge =
newValue; } public void setEmpName(String newValue){
empName = newValue; }
public void setEmpSSN(int newValue){ ssn = newValue; }
} public class EncapsTest{
public static void main(String args[]){
EncapsulationDemo obj = new EncapsulationDemo();
obj.setEmpName(“ilias"); obj.setEmpAge(38);
obj.setEmpSSN(112242); System.out.println("Employee
Name: " + obj.getEmpName());
System.out.println("Employee SSN: " +
obj.getEmpSSN()); System.out.println("Employee Age: "
+ obj.getEmpAge()); } }
17. The process by which one class acquires the properties(data members) and
functionalities(methods) of another class is called inheritance. The aim of inheritance
is to provide the reusability of code so that a class has to write only the unique
features and rest of the common properties and functionalities can be extended from
the another class.
Child Class:
The class that extends the features of another class is known as child class, sub class
or derived class.
Parent Class:
The class whose properties and functionalities are used(inherited) by another class is
known as parent class, super class or Base class.
Inheritance is a process of defining a new class based on an existing class by
extending its common data members and methods.
Inheritance allows us to reuse of code, it improves reusability in your java application.
Note: The biggest advantage of Inheritance is that the code that is already present in
base class need not be rewritten in the child class.
Syntax: Inheritance in Java
To inherit a class we use extends keyword. Here class XYZ is child class and class
ABC is parent class. The class XYZ is inheriting the properties and methods of ABC
class.
class XYZ extends ABC { }
18. Inheritance Example
class Teacher {
String designation = "Teacher";
String collegeName = “pub";
void does(){ System.out.println("Teaching"); } }
public class PhysicsTeacher extends Teacher{
String mainSubject = "Physics";
public static void main(String args[]){
PhysicsTeacher obj = new PhysicsTeacher();
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does(); } }
19. Types of inheritance
To learn types of inheritance in detail, refer:
Types of Inheritance in Java.
Single Inheritance: refers to a child and parent class relationship where a class extends the another class.
20. Polymorphism
This post provides the theoretical explanation of polymorphism with
real-life examples. For detailed explanation on this topic with java
programs refer polymorphism in java and runtime & compile time
polymorphism.
Polymorphism means to process objects differently based on their
data type.
In other words it means, one method with multiple implementation,
for a certain class of action. And which implementation to be used is
decided at runtime depending upon the situation (i.e., data type of
the object)
This can be implemented by designing a generic interface, which
provides generic methods for a certain class of action and there can
be multiple classes, which provides the implementation of these
generic methods.
21. Polymorphism could be static and dynamic both. Method Overloading is
static polymorphism while, Method overriding is dynamic polymorphism.
Overloading in simple words means more than one method having the
same method name that behaves differently based on the arguments
passed while calling the method. This called static because, which method
to be invoked is decided at the time of compilation
Overriding means a derived class is implementing a method of its super
class. The call to overriden method is resolved at runtime, thus called
runtime polymorphism
22. Types of polymorphism in java- Runtime and Compile time
polymorphism
types of polymorphism. There are two types of polymorphism in
java:
1) Static Polymorphism also known as compile time
polymorphism
2) Dynamic Polymorphism also known as runtime polymorphism
23. Compile time Polymorphism (or Static polymorphism)
Polymorphism that is resolved during compiler time is known as
static polymorphism. Method overloading is an example of
compile time polymorphism.
Method Overloading: This allows us to have more than one
method having the same name, if the parameters of methods are
different in number, sequence and data types of parameters. We
have already discussed Method overloading here: If you didn’t
read that guide, refer: Method Overloading in Java
24. Example of static Polymorphism
class SimpleCalculator {
int add(int a, int b) { return a+b; }
int add(int a, int b, int c) { return a+b+c; } }
public class Demo { public static void main(String
args[]) {
SimpleCalculator obj = new SimpleCalculator();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30)); } }
Output: 30 60
25. Runtime Polymorphism (or Dynamic polymorphism)
Dynamic polymorphism is a process in which a call to an
overridden method is resolved at runtime, thats why it is
called runtime polymorphism. I have already discussed
method overriding in detail in a separate tutorial, refer
it: Method Overriding in Java.
26. Example
In this example we have two classes ABC and
XYZ. ABC is a parent class and XYZ is a child
class. The child class is overriding the method
myMethod() of parent class.
27. class ABC{
public void myMethod(){
System.out.println("Overridden Method"); }
}
public class XYZ extends ABC{
public void myMethod(){
System.out.println("Overriding Method"); }
public static void main(String args[]){
ABC obj = new XYZ(); obj.myMethod(); } }
Design by ilias