Abstraction: OOPS Concepts
Abstraction: OOPS Concepts
Abstraction: OOPS Concepts
OOPS Concepts
1. Abstraction
Abstraction is the concept of hiding the internal details and describing things in
simple terms. For example, a method that adds two integers. The internal processing
of the method is hidden from the outer world. There are many ways to achieve
abstraction in object-oriented programmings, such as encapsulation and inheritance.
A Java program is also a great example of abstraction. Here java takes care of
converting simple statements to machine language and hides the inner
implementation details from the outer world.
package com.journaldev.oops.abstraction;
void turnOnCar();
void turnOffCar();
String getCarType();
}
package com.journaldev.oops.abstraction;
@Override
public void turnOnCar() {
System.out.println("turn on the manual car");
}
@Override
public void turnOffCar() {
System.out.println("turn off the manual car");
}
@Override
public String getCarType() {
return this.carType;
}
}
package com.journaldev.oops.abstraction;
@Override
public void turnOnCar() {
System.out.println("turn on the automatic car");
}
@Override
public void turnOffCar() {
System.out.println("turn off the automatic car");
}
@Override
public String getCarType() {
return this.carType;
}
}
User Program: Let’s look at a test program where the Car functions will be used.
package com.journaldev.oops.abstraction;
car1.turnOnCar();
car1.turnOffCar();
System.out.println(car1.getCarType());
car2.turnOnCar();
car2.turnOffCar();
System.out.println(car2.getCarType());
}
The client program only knows about the Car and the functions that the Car
provides. The internal implementation details are hidden from the client program.
2. Encapsulation
Encapsulation is the technique used to implement abstraction in object-oriented
programming. Encapsulation is used for access restriction to class members and
methods.
Access modifier keywords are used for encapsulation in object oriented
programming. For example, encapsulation in java is achieved
using private, protected and public keywords.
3. Polymorphism
Polymorphism is the concept where an object behaves differently in different
situations. There are two types of polymorphism – compile time polymorphism and
runtime polymorphism.
Compile-time polymorphism is achieved by method overloading. For example, we
can have a class as below.
public class Circle {
@Override
public void draw(){
System.out.println("Drwaing circle");
}
}
package com.journaldev.test;
@Override
public void draw() {
System.out.println("Drawing Square");
}
}
Shape is the superclass and there are two subclasses Circle and Square. Below
is an example of runtime polymorphism.
Shape sh = new Circle();
sh.draw();
In the above examples, java compiler doesn’t know the actual implementation class
of Shape that will be used at runtime, hence runtime polymorphism.
4. Inheritance
Inheritance is the object-oriented programming concept where an object is based on
another object. Inheritance is the mechanism of code reuse. The object that is
getting inherited is called the superclass and the object that inherits the superclass is
called a subclass.
We use extends keyword in java to implement inheritance. Below is a simple
example of inheritance in java.
package com.journaldev.java.examples1;
class SuperClassA {
a.foo();
a.bar();
}
}
Inheritance in Java
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the
mechanism in java by which one class is allow to inherit the features(fields and methods) of
another class.
Important terminology:
Super Class: The class whose features are inherited is known as super class(or a base
class or a parent class).
Sub Class: The class that inherits the other class is known as sub class(or a derived
class, extended class, or child class). The subclass can add its own fields and methods
in addition to the superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to
create a new class and there is already a class that includes some of the code that we
want, we can derive our new class from the existing class. By doing this, we are reusing
the fields and methods of the existing class.
How to use inheritance in Java
The keyword used for inheritance is extends.
Syntax :
class derived-class extends base-class
{
//methods and fields
}
Example: In below example of inheritance, class Bicycle is a base class, class MountainBike is
a derived class which extends Bicycle class and class Test is a driver class to run program.
In above program, when an object of MountainBike class is created, a copy of the all
methods and fields of the superclass acquire memory in this object. That is why, by using
the object of the subclass we can also access the members of a superclass.
Please note that during inheritance only object of subclass is created, not the superclass. For
more, refer Java Object Creation of Inherited Class.
Multithreading
Multithreading is a Java feature that allows concurrent execution of two or more parts of a
program for maximum utilization of CPU. Each part of such program is called a thread. So,
threads are light-weight processes within a process.
We create a class that extends the java.lang.Thread class. This class overrides the run()
method available in the Thread class. A thread begins its life inside run() method. We create
an object of our new class and call start() method to start the execution of a thread. Start()
invokes the run() method on the Thread object.
Thread 8 is running
Thread 9 is running
Thread 10 is running
Thread 11 is running
Thread 12 is running
Thread 13 is running
Thread 14 is running
Thread 15 is running
Thread creation by implementing the Runnable Interface
We create a new class which implements java.lang.Runnable interface and override run()
method. Then we instantiate a Thread object and call start() method on this object.
Thread 8 is running
Thread 9 is running
Thread 10 is running
Thread 11 is running
Thread 12 is running
Thread 13 is running
Thread 14 is running
Thread 15 is running
1. If we extend the Thread class, our class cannot extend any other class because Java
doesn’t support multiple inheritance. But, if we implement the Runnable interface, our class
can still extend other base classes.
2. Multiprocessing –
In a uni-processor system, only one process executes at a time.
Multiprocessing is the use of two or more CPUs (processors) within a single Computer
system. The term also refers to the ability of a system to support more than one processor
within a single computer system. Now since there are multiple processors available,
multiple processes can be executed at a time. These multi processors share the computer
bus, sometimes the clock, memory and peripheral devices also.
As depicted in the above image, At any time the CPU is executing only one task while other
tasks are waiting for their turn. The illusion of parallelism is achieved when the CPU is
reassigned to another task. i.e all the three tasks A, B and C are appearing to occur
simultaneously because of time sharing.
So for multitasking to take place, firstly there should be multiprogramming i.e. presence of
multiple programs ready for execution. And secondly the concept of time sharing.
4. Multi threading –
A thread is a basic unit of CPU utilization. Multi threading is an execution model that allows
a single process to have multiple code segments (i.e., threads) running concurrently within
the “context” of that process.
e.g. VLC media player, where one thread is used for opening the VLC media player, one
thread for playing a particular song and another thread for adding new songs to the playlist.
Multi threading is the ability of a process to manage its use by more than one user at a time
and to manage multiple requests by the same user without having to have multiple copies
of the program.
Immutable
Immutable class means that once an object is created, we cannot change its content. In
Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class is
immutable. We can create our own immutable class as well.
Following are the requirements:
The class must be declared as final (So that child classes can’t be created)
Data members in the class must be declared as final (So that we can’t change the
value of it after object creation)
A parameterized constructor
Getter method for all the variables in it
No setters(To not have the option to change the value of the instance variable)
Example to create Immutable class
// An immutable class
public final class Student
{
final String name;
final int regNo;
public Student(String name, int regNo)
{
this.name = name;
this.regNo = regNo;
}
public String getName()
{
return name;
}
public int getRegNo()
{
return regNo;
}
}
// Driver class
class Test
{
public static void main(String args[])
{
Student s = new Student("ABC", 101);
System.out.println(s.getName());
System.out.println(s.getRegNo());
// Uncommenting below line causes error
// s.regNo = 102;
}
}
Output:
ABC
101
In this example, we have created a final class named Student. It has two final data members,
a parameterized constructor and getter methods. Please note that there is no setter method
here.
(In order to make it functional, create objects of Student class in the main function.)
Enum
Enumerations serve the purpose of representing a group of named constants in a
programming language. For example the 4 suits in a deck of playing cards may be 4
enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type
named Suit. Other examples include natural enumerated types (like the planets, days of the
week, colors, directions, etc.).
Enums are used when we know all possible values at compile time, such as choices on a
menu, rounding modes, command line flags, etc. It is not necessary that the set of constants
in an enum type stay fixed for all time.
In Java (from 1.5), enums are represented using enum data type. Java enums are more
powerful than C/C++ enums . In Java, we can also add variables, methods and constructors
to it. The main objective of enum is to define our own data types(Enumerated Data Types).
Declaration of enum in java :
Enum declaration can be done outside a Class or inside a Class but not inside a
Method.
Design Pattern
https://www.tutorialspoint.com/design_pattern/design_pattern_overview.htm
Java Collections
https://www.edureka.co/blog/java-collections/
https://www.geeksforgeeks.org/collections-in-java-2/
https://www.javatpoint.com/collections-in-java
jvm memory
https://www.javatpoint.com/memory-management-in-java
https://www.geeksforgeeks.org/java-memory-management/
Object
https://www.tutorialspoint.com/java/java_object_classes.htm#:~:text=Object
%20%E2%88%92%20Objects%20have%20states%20and,object%20of%20its%20type%20support.
https://www.javatpoint.com/object-and-class-in-java
https://www.geeksforgeeks.org/classes-objects-java/
https://www.guru99.com/java-oops-class-objects.html
Thread
https://www.tutorialspoint.com/java/java_multithreading.htm
https://beginnersbook.com/2013/03/java-threads/
https://www.javatpoint.com/creating-thread
String Pool
https://www.journaldev.com/797/what-is-java-string-pool
https://www.edureka.co/blog/java-string-pool/
https://www.baeldung.com/java-string-pool
Garbage Collection
https://www.geeksforgeeks.org/garbage-collection-java/
https://www.javatpoint.com/Garbage-Collection