Java OOPs Concepts-notes
Java OOPs Concepts-notes
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.
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.
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.
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.
Definition OOP stands for Object Oriented POP stands for Procedural
1
Programing. Oriented Programming.
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.
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.