0% found this document useful (0 votes)
6 views29 pages

Java: Not a Pure OOP Language Explained

Java is not considered a pure object-oriented language due to its use of primitive data types, which are faster than objects. However, it supports object-oriented programming principles such as inheritance, encapsulation, abstraction, and polymorphism. The document also explains the structure of OOP, including classes, objects, methods, and attributes, along with examples of overloading and overriding methods.

Uploaded by

kafia.319300
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)
6 views29 pages

Java: Not a Pure OOP Language Explained

Java is not considered a pure object-oriented language due to its use of primitive data types, which are faster than objects. However, it supports object-oriented programming principles such as inheritance, encapsulation, abstraction, and polymorphism. The document also explains the structure of OOP, including classes, objects, methods, and attributes, along with examples of overloading and overriding methods.

Uploaded by

kafia.319300
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

Java is not a "PURE" Object Oriented Language , because it uses primitive data

types such as (int,float,char...etc). The developers of java could have made these
primitive data types as objects(like String... etc), but the primitive data types such
as int float... are more faster than objects! So, in order to achieve faster execution
of java program they kept Primitive data type as it is!!
So Java is said to be 98% Object oriented language but not PURE Object Oriented
language!!
Java is not pure object oriented programming language just because of primitive
data types like byte, short, int, char, float, double, long, boolean etc. we can work
with primitive type if we don't want to work with Object type.

why java is object oriented programming language?


Java Language is considered an object-oriented language because it is based on the
concept of objects and classes. Without the creation of objects and classes, it is
impossible to write any code in Java. Java supports the concepts of OOPS -
Inheritance, Data abstraction, polymorphism, and data encapsulation.

What is object-oriented programming?


Object-oriented programming (OOP) is a computer programming model that
organizes software design around data, or objects, rather than functions and logic.
An object can be defined as a data field that has unique attributes and behavior.

OOP focuses on the objects that developers want to manipulate rather than the
logic required to manipulate them. This approach to programming is well-suited
for programs that are large, complex and actively updated or maintained. This
includes programs for manufacturing and design, as well as mobile applications;
for example, OOP can be used for manufacturing system simulation software.

What is the structure of object-oriented programming?


The structure, or building blocks, of object-oriented programming include the
following:
Classes are user-defined data types that act as the blueprint for individual
objects, attributes and methods.

Objects are instances of a class created with specifically defined data.


Objects can correspond to real-world objects or an abstract entity. When class
is defined initially, the description is the only object that is defined.

Methods are functions that are defined inside a class that describe the
behaviors of an [Link] use methods for reusability or keeping
functionality encapsulated inside one object at a time.
Attributes are defined in the class template and represent the state of an
object. Objects will have data stored in the attributes field.

What is Encapsulation in java?

Encapsulation in Java is the process by which data (variables) and the code that
acts upon them (methods) are integrated as a single unit. By encapsulating a class's
variables, other classes cannot access them, and only the methods of the class can
access them.
What is abstraction in java?

In Java, Data Abstraction is defined as the process of reducing the object to its
essence so that only the necessary characteristics are exposed to the users.
Abstraction defines an object in terms of its properties (attributes), behavior
(methods), and interfaces (means of communicating with other objects).
What is inheritance in java?

Inheritance is a mechanism wherein a new class is derived from an existing class.


In Java, classes may inherit or acquire the properties and methods of other classes.
A class derived from another class is called a subclass, whereas the class from
which a subclass is derived is called a superclass.
What is polymorphism in java?

Polymorphism means "many forms", and it occurs when we have many


classes that are related to each other by inheritance.
Overriding and overloading in java :

What is Overloading and Overriding? When two or more methods in the same
class have the same name but different parameters, it's called Overloading.
When the method signature (name and parameters) are the same in the
superclass and the child class, it's called Overriding.
Constructor overloading in Java refers to the use of more than one constructor
in an instance class.

class GFG {

// Main driver method

public static void main(String[] args)

int i = 100;

// Automatic type conversion

// Integer to long type

long l = i;

// Automatic type conversion

// long to float type

float f = l;

// Print and display commands

[Link]("Int value " + i);

[Link]("Long value " + l);

[Link]("Float value " + f);

}
******************************************************************

// Java program to Illustrate Explicit Type Conversion

// Main class

public class GFG {

// Main driver method

public static void main(String[] args)

// Double datatype

double d = 100.04;

// Explicit type casting by forcefully getting

// data from long datatype to integer type

long l = (long)d;

// Explicit type casting

int i = (int)l;

// Print statements

[Link]("Double value " + d);

// While printing we will see that

// fractional part lost

[Link]("Long value " + l);

// While printing we will see that


// fractional part lost

[Link]("Int value " + i);

Output
Double value 100.04

Long value 100

Int value 100

Constructor :
// Java Program to illustrate calling a

// no-argument constructor

import [Link].*;

class Geek {

int num;

String name;

// this would be invoked while an object

// of that class is created.

Geek() { [Link]("Constructor called"); }

class GFG {

public static void main(String[] args)

// this would invoke default constructor.

Geek geek1 = new Geek();


// Default constructor provides the default

// values to the object like 0, null

[Link]([Link]);

[Link]([Link]);

// Java Program to Illustrate Working of

// Parameterized Constructor

// Importing required inputoutput class

import [Link].*;

// Class 1

class Geek {

// data members of the class.

String name;

int id;

// Constructor would initialize data members

// With the values of passed arguments while

// Object of that class created

Geek(String name, int id)

[Link] = name;

[Link] = id;

}
// Class 2

class GFG {

// main driver method

public static void main(String[] args)

// This would invoke the parameterized constructor.

Geek geek1 = new Geek("adam", 1);

[Link]("GeekName :" + [Link]

+ " and GeekId :" + [Link]);

Output

GeekName :adam and GeekId :1

*******
Overloading allows different methods to have the same name, but different signatures where the
signature can differ by the number of input parameters or type of input parameters or both.
Overloading is related to compile-time (or static) polymorphism.

// Java program to demonstrate working of method

// overloading in Java.

public class Sum {

// Overloaded sum(). This sum takes two int parameters

public int sum(int x, int y)

return (x + y);
}

// Overloaded sum(). This sum takes three int parameters

public int sum(int x, int y, int z)

return (x + y + z);

// Overloaded sum(). This sum takes two double parameters

public double sum(double x, double y)

return (x + y);

// Driver code

public static void main(String args[])

Sum s = new Sum();

[Link]([Link](10, 20));

[Link]([Link](10, 20, 30));

[Link]([Link](10.5, 20.5));

Output :

30

60

31.0

// A Simple Java program to demonstrate


// method overriding in java

// Base Class

class Parent {

void show()

[Link]("Parent's show()");

// Inherited class

class Child extends Parent {

// This method overrides show() of Parent

@Override

void show()

[Link]("Child's show()");

// Driver class

class Main {

public static void main(String[] args)

// If a Parent type reference refers

// to a Parent object, then Parent's

// show is called

Parent obj1 = new Parent();

[Link]();
// If a Parent type reference refers

// to a Child object Child's show()

// is called. This is called RUN TIME

// POLYMORPHISM.

Parent obj2 = new Child();

[Link]();

Output:

Parent's show()

Child's show()

Data Abstraction is the property by virtue of which only the essential details are displayed to the user.
The trivial or the non-essential units are not displayed to the user. Ex: A car is viewed as a car rather
than its individual components.

// Java program to illustrate the

// concept of Abstraction

abstract class Shape {

String color;

// these are abstract methods

abstract double area();

public abstract String toString();

// abstract class can have the constructor

public Shape(String color)

[Link]("Shape constructor called");


[Link] = color;

// this is a concrete method

public String getColor() { return color; }

class Circle extends Shape {

double radius;

public Circle(String color, double radius)

// calling Shape constructor

super(color);

[Link]("Circle constructor called");

[Link] = radius;

@Override double area()

return [Link] * [Link](radius, 2);

@Override public String toString()

return "Circle color is " + [Link]()

+ "and area is : " + area();

}
class Rectangle extends Shape {

double length;

double width;

public Rectangle(String color, double length,

double width)

// calling Shape constructor

super(color);

[Link]("Rectangle constructor called");

[Link] = length;

[Link] = width;

@Override double area() { return length * width; }

@Override public String toString()

return "Rectangle color is " + [Link]()

+ "and area is : " + area();

public class Test {

public static void main(String[] args)

Shape s1 = new Circle("Red", 2.2);

Shape s2 = new Rectangle("Yellow", 2, 4);


[Link]([Link]());

[Link]([Link]());

Output

Shape constructor called

Circle constructor called

Shape constructor called

Rectangle constructor called

Circle color is Redand area is : 15.205308443374602

Rectangle color is Yellowand area is : 8.0

Encapsulation vs Data Abstraction


1. Encapsulation is data hiding(information hiding) while Abstraction is
detailed hiding(implementation hiding).
2. While encapsulation groups together data and methods that act upon the
data, data abstraction deal with exposing the interface to the user and
hiding the details of implementation.
3. Encapsulated classes are java classes that follow data hiding and
abstraction while We can implement abstraction by using abstract classes
and interfaces.
4. Encapsulation is a procedure that takes place at the implementation level,
while abstraction is a design-level process.

5. // Java program to illustrate the


6. // concept of inheritance
7.
8. // base class
9. class Bicycle {
10. // the Bicycle class has two fields
11. public int gear;
12. public int speed;
13.
14. // the Bicycle class has one constructor
15. public Bicycle(int gear, int speed)
16. {
17. [Link] = gear;
18. [Link] = speed;
19. }
20.
21. // the Bicycle class has three methods
22. public void applyBrake(int decrement)
23. {
24. speed -= decrement;
25. }
26.
27. public void speedUp(int increment)
28. {
29. speed += increment;
30. }
31.
32. // toString() method to print info of Bicycle
33. public String toString()
34. {
35. return ("No of gears are " + gear + "\n"
36. + "speed of bicycle is " + speed);
37. }
38. }
39.
40. // derived class
41. class MountainBike extends Bicycle {
42.
43. // the MountainBike subclass adds one more field
44. public int seatHeight;
45.
46. // the MountainBike subclass has one constructor
47. public MountainBike(int gear, int speed,
48. int startHeight)
49. {
50. // invoking base-class(Bicycle) constructor
51. super(gear, speed);
52. seatHeight = startHeight;
53. }
54.
55. // the MountainBike subclass adds one more method
56. public void setHeight(int newValue)
57. {
58. seatHeight = newValue;
59. }
60.
61. // overriding toString() method
62. // of Bicycle to print more info
63. @Override public String toString()
64. {
65. return ([Link]() + "\nseat height is "
66. + seatHeight);
67. }
68. }
69.
70. // driver class
71. public class Test {
72. public static void main(String args[])
73. {
74.
75. MountainBike mb = new MountainBike(3, 100, 25);
76. [Link]([Link]());
77. }
78. }

Output
No of gears are 3
speed of bicycle is 100
seat height is 25

// Java program to illustrate the


// concept of single inheritance

import [Link].*;

import [Link].*;

import [Link].*;

class one {

public void print_geek()

[Link]("Geeks");

class two extends one {

public void print_for() { [Link]("for"); }

// Driver class

public class Main {

public static void main(String[] args)

two g = new two();


g.print_geek();

g.print_for();

g.print_geek();

Output
Geeks
for
Geeks

// Java program to illustrate the

// concept of Multilevel inheritance

import [Link].*;

import [Link].*;

import [Link].*;

class one {

public void print_geek()

[Link]("Geeks");

}
}

class two extends one {

public void print_for() { [Link]("for"); }

class three extends two {

public void print_geek()

[Link]("Geeks");

// Drived class

public class Main {

public static void main(String[] args)

three g = new three();

g.print_geek();

g.print_for();
g.print_geek();

Output
Geeks
for
Geeks

Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a


superclass (base class) for more than one subclass. In the below image, class A
serves as a base class for the derived class B, C and D.
 Java

// Java program to illustrate the

// concept of Hierarchical inheritance

class A {

public void print_A() { [Link]("Class A"); }

class B extends A {

public void print_B() { [Link]("Class B"); }

}
class C extends A {

public void print_C() { [Link]("Class C"); }

class D extends A {

public void print_D() { [Link]("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();

Output
Class A
Class B
Class A
Class C
Class A
Class D

Multiple Inheritance (Through Interfaces): In Multiple inheritances, one class


can have more than one superclass and inherit features from all parent classes.
Please note that Java does not support multiple inheritances with classes. In
java, we can achieve multiple inheritances only through Interfaces. In the image
below, Class C is derived from interface A and B.
 Java

// Java program to illustrate the

// concept of Multiple inheritance

import [Link].*;

import [Link].*;
import [Link].*;

interface one {

public void print_geek();

interface two {

public void print_for();

interface three extends one, two {

public void print_geek();

class child implements three {

@Override public void print_geek()

[Link]("Geeks");

}
public void print_for() { [Link]("for"); }

// Drived class

public class Main {

public static void main(String[] args)

child c = new child();

c.print_geek();

c.print_for();

c.print_geek();

Output
Geeks
for
Geeks

Java IS-A type of Relationship.


IS-A is a way of saying: This object is a type of that object. Let us see how the
extends keyword is used to achieve inheritance.

 Java
public class SolarSystem {

public class Earth extends SolarSystem {

public class Mars extends SolarSystem {

public class Moon extends Earth {

Now, based on the above example, in Object-Oriented terms, the following are
true:-
1. SolarSystem the superclass of Earth class.
2. SolarSystem the superclass of Mars class.
3. Earth and Mars are subclasses of SolarSystem class.
4. Moon is the subclass of both Earth and SolarSystem classes.
 Java

class SolarSystem {

class Earth extends SolarSystem {

class Mars extends SolarSystem {

}
public class Moon extends Earth {

public static void main(String args[])

SolarSystem s = new SolarSystem();

Earth e = new Earth();

Mars m = new Mars();

[Link](s instanceof SolarSystem);

[Link](e instanceof Earth);

[Link](m instanceof SolarSystem);

Output
true
true
true

// Java Program for Method Overriding

// Class 1

// Helper class
class Parent {

// Method of parent class

void Print()

// Print statement

[Link]("parent class");

// Class 2

// Helper class

class subclass1 extends Parent {

// Method

void Print() { [Link]("subclass1"); }

// Class 3
// Helper class

class subclass2 extends Parent {

// Method

void Print()

// Print statement

[Link]("subclass2");

// Class 4

// Main class

class GFG {

// Main driver method

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();

[Link]();

a = new subclass2();

[Link]();

Output:
subclass1
subclass2
why multiple inheritance is not supported in java?
The reason behind this is to prevent ambiguity. Consider a case where class B
extends class A and Class C and both class A and C have the same method
display(). Now java compiler cannot decide, which display method it should
inherit. To prevent such situation, multiple inheritances is not allowed in java.

why java is platform independent ?


Java is platform-independent because it uses a virtual machine. The Java
programming language and all APIs are compiled into bytecodes. Bytecodes are
effectively platform-independent. The virtual machine takes care of the
differences between the bytecodes for the different platforms.
why java is secure than other language?
The usage of security manager and class loader makes it easy for the Java run-
time to avoid any arbitrary code from executing by mediating access to the
system resources and preventing the program from loading or generating any
arbitrary code at run-time. Java provides library level safety.

You might also like