0% found this document useful (0 votes)
1 views

Java OOPs Concepts-notes

The document provides an overview of Object-Oriented Programming (OOP) concepts, including key principles such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation. It highlights the advantages of OOP over procedural programming and details features of Java, such as its simplicity, robustness, and platform independence. Additionally, the document includes examples of Java program structure and syntax, demonstrating how to define classes and methods.

Uploaded by

rishavthakkar02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java OOPs Concepts-notes

The document provides an overview of Object-Oriented Programming (OOP) concepts, including key principles such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation. It highlights the advantages of OOP over procedural programming and details features of Java, such as its simplicity, robustness, and platform independence. Additionally, the document includes examples of Java program structure and syntax, demonstrating how to define classes and methods.

Uploaded by

rishavthakkar02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java OOPs Concept

Object-Oriented Programming is a paradigm that provides many concepts such


as inheritance, data binding, polymorphism, etc. Simula is considered the first object-
oriented programming language. The programming paradigm where everything is
represented as an object is known as a truly object-oriented programming language.
Smalltalk is considered the first truly object-oriented programming language.The popular
object-oriented languages are Java, C#, PHP, Python, C++, etc.

The main aim of object-oriented programming is to implement real-world entities for


example object, classes, abstraction, inheritance, polymorphism, etc.

OOPs (Object-Oriented Programming System)

Object means a real-world entity such as a pen, chair, table, computer, watch,
etc. Object-Oriented Programming is a methodology or paradigm to design a
program using classes and objects. It simplifies the software development and
maintenance by providing some concepts:

o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation

Object
Any entity that has state and behaviour is known as an object. For example a chair, pen,
table, keyboard, bike, etc. It can be physical or logical.

An Object can be defined as an instance of a class. An object contains an address and


takes up some space in memory. Objects can communicate without knowing the details
of each other's data or code. The only necessary thing is the type of message accepted
and the type of response returned by the objects.

Example: A dog is an object because it has states like color, name, breed, etc. as well
as behaviors like wagging the tail, barking, eating, etc.

Class
Collection of objects is called class. It is a logical entity.

A class can also be defined as a blueprint from which you can create an individual object.
Class doesn't consume any space.

Inheritance
When one object acquires all the properties and behaviours of a parent object, it is
known as inheritance. It provides code reusability. It is used to achieve runtime
polymorphism.

Example- Classes like BTECH student, MTECH student, MCA student have some common
properties so they can be derived from super-class Student.

Polymorphism
If one task is performed by different ways, it is known as polymorphism. For example
calculate student-marks-calculation logic will be different for BTECH student, MTECH
student or MCA student. Another example can be to speak something; for example, a cat
speaks meow, dog barks woof, etc.

In Java, we use method overloading and method overriding to achieve polymorphism.

Abstraction
Hiding internal details and showing functionality is known as abstraction. In Java, we
use abstract class and interface to achieve abstraction.
For example phone call, we don't know the internal processing. Think of a stereo system
as an object with a complex logic board on the inside. It has buttons on the outside to
allow for interaction with the object. When you press any of the buttons, you're not
thinking about what happens on the inside because you can't see it. Even though you
can't see the logic board completing functions as a result of pressing a button, it's still
performing actions. This is an abstraction, which can be used to understand the concept
of programming.
Encapsulation
Binding (or wrapping) code and data together into a single unit are known as
encapsulation. For example capsule, it is wrapped with different medicines.

A java class is the example of encapsulation. Java bean is the fully encapsulated class
because all the data members are private here. Consider a class employee having name
and salary attributes. In that class we can have two methods for updating and viewing
salary. Any outsider method cant access the employee salary by virtue of encapsulation.

Advantage of OOPs over Procedure-oriented programming language


1) OOP makes development and maintenance easier whereas in a procedure-oriented
programming language it is not easy to manage if code grows as project size increases.

2) OOP provides data hiding whereas in a procedure-oriented programming language a


global data can be accessed from anywhere.
Figure: Data Representation in Procedure-Oriented Programming

Figure: Data Representation in Object-Oriented Programming

3) OOPs provides the ability to simulate real-world event much more effectively. We can
provide the solution of real word problem if we are using the Object-Oriented
Programming language.

Sr. Key OOP POP


No.

Definition OOP stands for Object Oriented POP stands for Procedural
1
Programing. Oriented Programming.

Approach OOP follows bottom up POP follows top down approach.


2
approach.

Division A program is divided to objects A program is divided into


3
and their interactions. funtions and they interacts.

Inheritance Inheritance is supported. Inheritance is not supported.


4
supported

Access Access control is supported via No access modifiers are


5
control access modifiers. supported.
Data Encapsulation is used to hide No data hiding present. Data is
6
Hiding data. globally accessible.

7 Example C++, Java C, Pascal

Features of Java
o Simple: Java is a simple language because its syntax is simple, clean, and easy
to understand. Complex and ambiguous concepts of C++ are either eliminated or
re-implemented in Java. For example, pointer and operator overloading are not
used in Java.
o Object-Oriented: In Java, everything is in the form of the object. It means it
has some data and behavior. A program must have at least one class and object.
o Robust: Java makes an effort to check error at run time and compile time. It
uses a strong memory management system called garbage collector. Exception
handling and garbage collection features make it strong.
o Secure: Java is a secure programming language because it has no explicit
pointer and programs runs in the virtual machine. Java contains a security
manager that defines the access of Java classes.
o Platform-Independent: Java provides a guarantee that code writes once and
run anywhere. This byte code is platform-independent and can be run on any
machine.
o Portable: Java Byte code can be carried to any platform. No implementation-
dependent features. Everything related to storage is predefined, for example, the
size of primitive data types.
o High Performance: Java is an interpreted language. Java enables high
performance with the use of the Just-In-Time compiler.
o Distributed: Java also has networking facilities. It is designed for the distributed
environment of the internet because it supports TCP/IP protocol. It can run over
the internet. EJB and RMI are used to create a distributed system.
o Multi-threaded: Java also supports multi-threading. It means to handle more
than one job a time.
Parts of a java program

Section Description

Package You can create a package with any name. A package is a group of classes
statement that are defined by a name. That is, if you want to declare many classes
within one element, then you can declare it within a package. It is an optional
part of the program, i.e., if you do not want to declare any package, then there
will be no problem with it, and you will not get any errors. Here, the package is
a keyword that tells the compiler that package has been created.

It is declared as:
package package_name;

Import This line indicates that if you want to use a class of another package, then
statements you can do this by importing it directly into your program.
Example:

import calc.add;

Interface Interfaces are like a class that includes a group of method declarations. It's an
statement optional section and can be used when programmers want to implement
multiple inheritances within a program.

Class A Java program may contain several class definitions. Classes are the main
Definition and essential elements of any Java program. This includes member variables,
constructors and methods.

Main Method Every Java stand-alone program requires the main method as the starting
Class point of the program. This is an essential part of a Java program. There may
be many classes in a Java program, and only one class defines the main
method. Methods contain data type declaration and executable statements.

Example 1-A Simple Java Program to Print "Hello Java"

//Name of this file will be "Hello.java"

public class Hello


{
public static void main(String[] args)
{
System.out.println("Hello Java");
}
}
Program Output:
Hello Java

Let's Look into Various Parts of the Above Java Program


public class Hello ● This creates a class called Hello.
● All class names must start with a capital letter.
● The public word means that it is accessible from any other classes.

public static void ● When the main method is declared public, it means that it can also be used by
main code outside of its class, due to which the main method is declared public.
● The word static used when we want to access a method without creating its
object, as we call the main method, before creating any class objects.
● The word void indicates that a method does not return a value. main() is
declared as void because it does not return a value.
● main is a method; this is a starting point of a Java program.
You will notice that the main method code has been moved to some spaces left.
It is called indentation which used to make a program easier to read and
understand.
String[] args It is an array where each element of it is a string, which has been named as
"args". If your Java program is run through the console, you can pass the input
parameter, and main() method takes it as input.

System.out.println(); This statement is used to print text on the screen as output, where the system is
a predefined class, and out is an object of the PrintWriter class defined in the
system. The method println prints the text on the screen with a new line. You can
also use print() method instead of println() method. All Java statement ends with
a semicolon.

Example 2-class Stusent

class Student //Type Declaration


{
int id=10; //field or data member or instance variable section
String name=”ABC”;
//Constructor section

public static void main(String args[]) //Method section-main method


{ Student s1=new Student();…………………
……………//creating an object of Student

System.out.println(s1.id); //accessing member through reference variable


System.out.println(s1.name);
}
}

You might also like