Core JAVA
DEFINATION OF OOPS IN JAVA
“Object-oriented programming is an approach that modularizes
programs by creating a partitioned memory area for both
functions and data that can be used as templates for creating
copies of such modules on demand.”
OOPS Objective
The primary objective of the object-oriented approach is to
eliminate some of the pitfalls that exist in the procedural
approach. OOP treats data as an element in the program, not
allowing it to flow around the system freely. It ties data closely
to the functions that operate on it and protects it from
unintentional modification by other existing functions. OOPS
allows decomposing a problem into several entities called
Objects and then build data and functions from these entities.
The combination of the data makes up an object
Object = Method + Data
The data of an object is accessed by the methods associated with
that object. However, the methods of an object can access
Features of OOPS
Emphasis is on data than procedures
Programs are divided into objects
Data Structures are designed to characterize objects.
Methods operating on the data of an object are tied
together in the data structure.
Data is hidden, and external functions cannot access it.
Objects communicate with each other through methods
New methods and data can be easily added whenever
necessary
Follows the bottom-up approach in program design
OBJECT
Object: is a bundle of data and its behaviour(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
So if I had to write a class based on states and behaviours of
House. I can do it like this: States can be represented as instance
variables and behaviours as methods of the class. We will see how
to create classes in the next section of this guide.
class House {
String address;
String color;
double are;
void openDoor() {
//Write code here
}
void closeDoor() {
//Write code here
}
...
...
}
CHARACTERISTIC OF OBJECTS
If you find it hard to understand Abstraction and Encapsulation,
do not worry as I have covered these topics in detail with
examples in the next section of this guide.
1. Abstraction
2. Encapsulation
3. Message passing
Abstraction: Abstraction is a process where you show only
“relevant” data and “hide” unnecessary details of an object from
the user.
Encapsulation: Encapsulation simply means binding object
state(fields) and behaviour(methods) together. If you are creating
class, you are doing encapsulation.
Message passing
A single object by itself may not be very useful. An application
contains many objects. One object interacts with another object
by invoking methods on that object. It is also referred to
as Method Invocation.
Class In OOPs:
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.
OBJECT and CLASS EXAMPLE: MAIN OUTSIDE THE CLASS
In real time development, we create classes and use it from
another class. It is a better approach than previous one. Let's see a
simple example, where we are having main() method in another
class.
We can have multiple classes in different Java files or single Java
file. If you define multiple classes in a single Java source file, it is a
good idea to save the file name with the class name which has
main() method.
//Java Program to demonstrate having the main method i n
//another class
//Creating Student class.
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main
method
class TestStudent1{
public static void main(String args[]){ Student s1=new Student();
System.out.println(s1.id); System.out.println(s1.name);
}
}
Object and Class Example: Initialization through reference
Initializing an object means storing data into the object. Let's see
a simple example where we are going to initialize the object
through a reference variable.
class Student{
int id;
String name;
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student(); s1.id=101; s1.name="Sonoo";
System.out.println(s1.id+" "+s1.name);//printing member s with
a white space }
}
CONSTRUCTOR IN JAVA
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 MyClass to create a new object (or
instance).
We can also have parameters in the constructor, such
constructors are known as parametrized constructors.
Four features are the main OOPs Concepts that you must learn to
understand the Object Oriented Programming in Java
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
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 bank account online, you enter
your user_id and password and press login, what happens when
you press login, how the input data sent to server, how it gets
verified is all abstracted away from the you.
ENCAPSULATION
Encapsulation simply means binding object state(fields) and
behavior(methods) together. If you are creating class, you are
doing encapsulation.
INHERITANCE
The process by which one class acquires the properties and
functionalities of another class is called inheritance. Inheritance
provides the idea of reusability of code and each sub class defines
only those features that are unique to it, rest of the features can
be inherited from the parent 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.
The parent class is called the base class or super class. The
child class that extends the base class is called the derived
class or sub class or child class.
Syntax: Inheritance in Java
To inherit a class we use extends keyword. Here class A is child
class and class B is parent class.
class A extends B
{
}
POLYMORPHISM
Polymorphism is a object oriented programming feature that
allows us to perform a single action in different ways. For
example, lets say we have a class Animal that has a method
animalSound(), here we cannot give implementation to this
method as we do not know which Animal class would extend
Animal class. So, we make this method abstract like this:
public abstract class Animal{
...
public abstract void animalSound();
}
Now suppose we have two Animal classes Dog and Lion that
extends Animal class. We can provide the implementation detail
there.
public class Lion extends Animal{
...
@Override
public void animalSound(){
System.out.println("Roar");
}
}
and
public class Dog extends Animal{
...
@Override
public void animalSound(){
System.out.println("Woof");
}
}
Types of Polymorphism
1) Static Polymorphism
2) Dynamic Polymorphism
Static Polymorphism:
Polymorphism that is resolved during compiler time is known as
static polymorphism. Method overloading can be considered as
static polymorphism example.
Method Overloading: This allows us to have more than one
methods with same name in a class that differs in signature.
Dynamic Polymorphism
It is also known as Dynamic Method Dispatch. Dynamic
polymorphism is a process in which a call to an overridden
method is resolved at runtime rather, thats why it is called
runtime polymorphism.
INTERFACE IN JAVA
An interface in Java is a blueprint of a class. It has static constants
and abstract methods.
The interface in Java is a mechanism to achieve abstraction.
There can be only abstract methods in the Java interface, not
method body. It is used to achieve abstraction and
multiple inheritance in Java.
In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body.
Java Interface also represents the IS-A relationship.
It cannot be instantiated just like the abstract class.
Since Java 8, we can have default and static methods in an
interface.
Since Java 9, we can have private methods in an interface.
There are mainly three reasons to use interface.
They are given below.
• It is used to achieve abstraction.
• By interface, we can support the functionality of multiple
inheritance.
• It can be used to achieve loose coupling.
How to declare an interface?
An interface is declared by using the interface keyword. It
provides total abstraction; means all the methods in an interface
are declared with the empty body, and all the fields are public,
static and final by default. A class that implements an interface
must implement all the methods declared in the interface.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
JAVA INTERFACE EXAMPLE
In this example, the Printable interface has only one method, and
its implementation is provided in the A6 class.
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){ A6 obj = new A6();
obj.print();
}
}
Multiple inheritance in Java by interface
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){ A7 obj = new A7();
obj.print();
obj.show();
}
}
INTERFACE INHERITANCE
A class implements an interface, but one interface extends
another interface.
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){ TestInterface4 obj = new
TestInterface4(); obj.print();
obj.show();
}
}