0% found this document useful (0 votes)
25 views9 pages

OOP's Concept in JAVA

The document explains the Object-Oriented Programming (OOP) concepts in Java, detailing key characteristics such as Class, Object, Abstraction, Encapsulation, Inheritance, and Polymorphism. It provides examples for each concept, illustrating how they are implemented in Java code. Additionally, it covers different types of inheritance and polymorphism, including method overloading and overriding.

Uploaded by

samiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views9 pages

OOP's Concept in JAVA

The document explains the Object-Oriented Programming (OOP) concepts in Java, detailing key characteristics such as Class, Object, Abstraction, Encapsulation, Inheritance, and Polymorphism. It provides examples for each concept, illustrating how they are implemented in Java code. Additionally, it covers different types of inheritance and polymorphism, including method overloading and overriding.

Uploaded by

samiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

OOP’s Concept in JAVA

As the name suggests, Object-Oriented Programming or Java OOPs concept refers to languages that use
objects in programming. The different characteristics of an Object-Oriented Programming Language are
as follows,

1. Class
2. Object
3. Abstraction
4. Encapsulation
5. Inheritance
6. Polymorphism

1. Class:
A class is a user-defined blueprint or prototype from which objects are created. It represents the
set of properties or methods that are common to all objects of one type.
Example:
Create a class named "Main" with a variable x:

public class Main {


int x = 5;
}

2. Object:
An object is a basic unit of Object-Oriented Programming that represents real-life entities. A typical Java
program creates many objects, which as you know, interact by invoking methods.
Example:
Create an object called "myObj" and print the value of x:
public class Main {
int x = 5;

public static void main(String[] args) {


Main myObj = new Main();
System.out.println(myObj.x);
}
}

3. Abstraction:
Data abstraction is the process of hiding certain details and showing only essential information
to the user. Abstraction can be achieved with either abstract classes or interfaces. Data Abstraction
may also be defined as the process of identifying only the required characteristics of an object,
ignoring the irrelevant details. The properties and behaviors of an object differentiate it from other
objects of similar type and also help in classifying the object.
Example:
An abstract class can have both abstract and regular methods:

abstract class Animal {


public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}

4. Encapsulation:
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To
achieve this, you must:
 Declare class variables/attributes as private.
 Provide public get and set methods to access and update the value of a private variable.
Example:
public class Person {
private String name; // private = restricted access

// Getter
public String getName() {
return name;
}

// Setter
public void setName(String newName) {
this.name = newName;
}
}
public class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.setName("John"); // Set the value of the name variable to "John"
System.out.println(myObj.getName());
}
}

5. Polymorphism:
Polymorphism is considered one of the important features of Object-Oriented Programming.
Polymorphism allows us to perform a single action in different ways. In other words, polymorphism
allows you to define one interface and have multiple implementations. The word “poly” means
many and “morphs” means forms.
Types of Java Polymorphism
In Java Polymorphism is mainly divided into two types:
 Compile-time Polymorphism
 Runtime Polymorphism

Method Overloading
When there are multiple functions with the same name but different parameters then these
functions are said to be overloaded. Functions can be overloaded by changes in the number of
arguments or/and a change in the type of arguments.
Example:
class Helper {

// Method with 2 integer parameters


static int Multiply(int a, int b)
{
return a * b;
}

// With same name but with 2 double parameters


static double Multiply(double a, double b)
{
return a * b;
}
}

// Main class
class Main {
public static void main(String[] args)
{
System.out.println(Helper.Multiply(2, 4));
System.out.println(Helper.Multiply(5.5, 6.3));
}
}

Method overriding
Method overriding, on the other hand, occurs when a derived class has a definition for one of
the member functions of the base class. That base function is said to be overridden.
Example:
class Parent {
void Print()
{
System.out.println("parent class");
}
}

class subclass1 extends Parent {


void Print() { System.out.println("subclass1"); }
}

class subclass2 extends Parent {


void Print()
{
System.out.println("subclass2");
}
}

class Main {

public static void main(String[] args)


{
// Creating object of class 1
Parent a;
// Now we will be calling print methods
// inside main() method
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
}
}
6. Inheritance:
Java, Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in
Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java,
Inheritance means creating new classes based on existing ones. A class that inherits from another class
can reuse the methods and fields of that class. In addition, you can add new fields and methods to your
current class as well.
Need of Java Inheritance:
 Code Reusability: The code written in the Superclass is common to all subclasses. Child classes
can directly use the parent class code.
 Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the
ways by which Java achieves Run Time Polymorphism.
 Abstraction: The concept of abstract where we do not have to provide all details, is achieved
through inheritance. Abstraction only shows the functionality to the user.
Java Inheritance Types
Below are the different types of inheritance which are supported by Java.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance

Single Inheritance:

Example:
import java.io.*;
import java.lang.*;
import java.util.*;

// Parent class
class One {
public void print_geek()
{
System.out.println("Geeks");
}
}

class Two extends One {


public void print_for() { System.out.println("for"); }
}
// Driver class
public class Main {
// Main function
public static void main(String[] args)
{
Two g = new Two();
g.print_geek();
g.print_for();
g.print_geek();
}
}

Multilevel Inheritance:

Example:

import java.io.*;
import java.lang.*;
import java.util.*;

// Parent class One


class One {
public void print_geek() {
System.out.println("Geeks");
}
}

// Child class Two inherits from class One


class Two extends One {
public void print_for() {
System.out.println("for");
}
}

// Child class Three inherits from class Two


class Three extends Two {
public void print_lastgeek() {
System.out.println("Geeks");
}
}

public class Main {


public static void main(String[] args) {
// Creating an object of class Three
Three g = new Three();
// Calling method from class One
g.print_geek();
// Calling method from class Two
g.print_for();
// Calling method from class Three
g.print_lastgeek();
}
}

Hierarchical Inheritance:

Example:

class A {
public void print_A() {
System.out.println("Class A");
}
}

class B extends A {
public void print_B() {
System.out.println("Class B");
}
}

class C extends A {
public void print_C() {
System.out.println("Class C");
}
}

class D extends A {
public void print_D() {
System.out.println("Class D");
}
}

// Driver Class
public class Test {
public static void main(String[] args)
{
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();

C obj_C = new C();


obj_C.print_A();
obj_C.print_C();

D obj_D = new D();


obj_D.print_A();
obj_D.print_D();
}
}

Hybrid Inheritance:

Example:
class C
{
public void disp()
{
System.out.println("C");
}
}

class A extends C
{
public void disp()
{
System.out.println("A");
}
}

class B extends C
{
public void disp()
{
System.out.println("B");
}

class D extends A
{
public void disp()
{
System.out.println("D");
}
public static void main(String args[]){

D obj = new D();


obj.disp();
}
}

You might also like