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

Java Oops Concepts1

The document discusses key concepts in object-oriented programming (OOP) including objects, classes, inheritance, polymorphism, abstraction, encapsulation, dynamic binding, and message passing. Objects are instances of classes that contain data fields and methods. Classes act as blueprints for objects. Inheritance allows classes to inherit properties from parent classes. Polymorphism allows objects to take different forms. Abstraction hides unnecessary details. Encapsulation bundles data and methods into a single unit.

Uploaded by

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

Java Oops Concepts1

The document discusses key concepts in object-oriented programming (OOP) including objects, classes, inheritance, polymorphism, abstraction, encapsulation, dynamic binding, and message passing. Objects are instances of classes that contain data fields and methods. Classes act as blueprints for objects. Inheritance allows classes to inherit properties from parent classes. Polymorphism allows objects to take different forms. Abstraction hides unnecessary details. Encapsulation bundles data and methods into a single unit.

Uploaded by

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

OOPS CONCEPTS

• The object is Real-world entities i.e Pen, Paper, computer, watch, fan,
etc.
• Object-Oriented Programming is a methodology to implement real-
world entities by designing a program using classes and objects.
• OOPS(Object-Oriented programming System) provides a paradigm to
simplifies the development and maintenance of a Software.
Main concepts of OOPS

• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
• Dynamic binding
• Message passing
OBJECT

• A physical or logical entity that has state and behavior is known


as an object. For Example Pen, Paper, computer, watch fan, etc.
• Object is a unique entity which contains data and functions(state
and behaviour) together in an OOP language.
• An Object is an instance of a class. It contains an address and
takes some space in heap memory.
• For Example, A car is an object because it has states like color,
make, model, engine, etc. as well as behaviors like speed,
accelerator, music, etc.
• Examples of states and behaviors
Example 1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
• Software program does not deal with real world objects. The program uses software objects.
• The states of a real world object are considered as the data members of the software object
• In the same way, Behaviour is considered as the methods or functions in software objects.
• An object contains member variables to hold data values
• The data values of an object can be defined as the state
• The behaviour can be implemented through member functions(methods).
CLASS
• The collection of objects is called a class.
• Class is the blueprint of an object from which object created.
• Class doesn’t consume space.
• A class has properties and behaviors i.e methods that are common to all
objects of the same type.
• Class is known as OBJECT FACTORY
ABSTRACTION

• Abstraction refers to an act of representing essential features without


including background details.
• Example: 1. we operate ATM machine but don’t know about internal
mechanism
• 2. In order to drive a car we are only aware of some essential external
features like steering,accelerator,clutch,brake,gears… but unaware of the
mechanism of engine of a car.
• Abstraction can be achieved by
• Abstract Class and abstract method
• Interface
ENCAPSULATION

• Wrapping of data and functions into a single unit.


• Encapsulation is used for access restriction to class members and methods.
• Access modifiers keywords are used for encapsulation in object oriented
programming. For example, encapsulation in java is achieved using public, private and
protected.
REAL TIME EXAMPLE OF ENCAPSULATION

• one of the real world example of encapsulation is Capsule,


as capsule binds all it's medicinal materials within it,
similarly in java encapsulation units(class, interface, enums
etc) encloses all it's data and behavior within it.
• Another real world example can be your school or office
bag. The bag contains different stuffs like pen, pencil,
notebook etc within it, in order to get any stuff you need to
open that bag, similarly in java an encapsulation unit
contains it's data and behavior within it and in order to
access them you need an object of that unit.
INHERITANCE

• Inheritance is a process where child class acquires all the properties


and behaviors of the parent class.
• Inheritance is used when one object is based on another object.
• Here parent class also called a superclass and child class called as a
subclass.
• For Example, Person is Parent class and Employee is a subclass of
Person. which acquired all the properties and behavior of Person
class.
JAVA does not support multiple inheritance.
• It is implemented with the help of interfaces
POLYMORPHISM

• Polymorphism is the concept where an object behaves differently in different


situations.
• There are two types of polymorphism – compile time polymorphism and runtime
polymorphism.
• Polymorphism is the process of using a function for more
than one purpose.
• It allows the use of different internal structure of the object
by keeping the same external surface
METHOD 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, but implementation
is different it's called Overriding.
REAL TIME EXAMPLE OF POLYMORPHISM

• Suppose if you are in class room that time you behave like a student,
when you are in market at that time you behave like a customer,
when you at your home at that time you behave like a son or
daughter, Here one person present in different-different behaviors.
• Sometime your mobile behaves as a phone, sometime
as a camera, sometime as a radio etc. Here the same
mobile phone has different forms, so we can say the
mobile object is polymorphic in nature.
MESSAGE PASSING
DYNAMIC BINDING

Process that links the function call with function definition at


runtime during execution of program
ABSTRACT CLASS

• Abstract class is a class with ‘abstract’ keyword


• atleast one abstract method.
• We cannot create objects for the abstract classes.
ABSTRACT METHODS

• Abstract Methods in Java

• Abstract methods are methods with no implementation and without a method body. They do not
contain any method statement.

• An abstract method is declared with an abstract keyword.

• The declaration of an abstract method must end with a semicolon ;

• The child classes which inherit the abstract class must provide the implementation
of these inherited abstract methods.

• access-specifier abstract return-type method-name();


• abstract class BaseClass
•{
• //abstract method
• abstract public void show1();
• //concrete method
• public void show2()
• {
• System.out.println("Concrete method of parent class");
• }
•}
• //child class

• class ChildClass extends BaseClass

• {

• // Must Override this method while extending the Parent class

• public void show1()

• {

• System.out.println("Overriding the abstract method of the parent class");

• }

• //Overriding concrete method is not compulsory

• public voidshow2()

• {
INTERFACES

• Defining Interfaces:

• Interface is a kind of a class but contain only final variables and abstract
methods.

• Interfaces do not specify any code to implement these methods and data fields contain
only constants.

• It is the responsibility of the class that implements an interface to define the code
for implementation of these methods.
• Syntax:

• interface <interfacename>

• {

• final variables; //final int x=10;

• abstract methods; // abstract public void show1();

• }
FINAL keyword

• The final keyword declared with variable, method, and class indicates that “This cannot be
modified”.
• Final is a keyword that is used to restrict the user in Java programming.
• Java Final keyword has three different uses:
1. To create constant.
2. To prevent inheritance.
3. To prevent method from being overridden.
Final key word
Difference between abstract class
and interface
constructor
Garbage collection in java

• Garbage Collection is process of reclaiming the runtime unused


memory automatically.
• In other words, it is a way to destroy the unused objects.
• To do so, we were using free() function in C language and
delete() in C++.
• But, in java it is performed automatically.
Garbage collection

• In Java, garbage collection is the process of managing


memory, automatically.
• It finds the unused objects (that are no longer used by the
program) and delete or remove them to free up the memory.
• The garbage collection mechanism uses several GC algorithms.
• The most popular algorithm that is used is Mark and Sweep.
Platform Independent

• In Java, the source code is compiled and converted


into the byte code. This byte code is not the machine
code.
• So for the required machine code we have the Java
Virtual Machine in Java. It interprets the byte code and
converts it into the machine specific code.
• Java Virtual Machine is different for each platform;
that is why it is known as platform-dependent.
• You can write your code once and then run it
anywhere, on any platform that provides the
environment to run it. This environment is the Java
Virtual Machine (JVM). The JVM should be present to
execute the code.
Jdk,jre,jvm
• JRE(Java Runtime Environment) is the environment
JVM(Java Virtual Machine) is an abstract machine. within which the JVM runs
It is a specification that provides runtime environment in • It contains a set of libraries+other files that JVM uses
which java bytecode can be executed. at run time
• JVM is an abstract machine that doesn’t exist
physically • JDK(Java Development Kit) comes with collection of
tools that are used for developing and running Java
• It provides runtime environment to drive the java code programs which includes:

• It is platform dependent 1. appletviewer(for viewing applets)


2. javac(java compiler)
• JVM executes the byte code
3. java(java interpreter)
4. javah(for c headerfiles)
5. javadoc(for HTML documents)
6. jdb(java debugger)
Application Programming Interface

• The java Standard library (API) includes lots of classes and packages
1. Language support package.
2. Utilities package
3. Input/output package
4. Networking package
5. AWT package
6. Applet package
JAVA Vs C

• C is a procedural language and Java is object oriented language


• Java does not include keywords sizeof and typedef
• Java does not define the keywords auto,extern,register,signed and unsigned
• Java doesnot support an explicit pointer type
• Java doesnot have a pre-processor(#define,#include)
• Java requires that the functions with no arguments must be declared with empty
parantheses and not with void keyword as done in C
INTRODUCTION

• Array is a group of continuous or related data items that


share a common name.
• Syntax:
array_name[value];
• Example:
salary[10];
DECLARATION OF ARRAYS
• Arrays in Java may be declared in two forms:
Form1 type arrayname[ ];

Form2 Type[ ] arrayname;


Example:
int number[ ];
float average[ ];
int[ ] counter;
float[ ] marks;
CREATION OF ARRAYS

• Java allows to create arrays using new operator only , as


shown below:
arrayname = new type[size];

• Examples:
number = new int[5];
average = new float[10];
INITIALIZATION OF ARRAYS

 In this step values are put into the array created. This process is
known as initialization.
arrayname[subscript] = value;

 Example:
number[0]=35;
number[1]=40;
.............
number[n]=19;
• int[ ] primes = {2, 3, 5, 7, 11, 13, 17};
Points to ponder:

• Java creates array starting with a subscript of 0 and ends


with a value less than the size specified.
• Trying to access an array beyond its boundaries will
generate an error message.
• Array can also be initialize as the other ordinary variables
when they are declared.
• Array initializer is a list of values separated by commas and
surrounded by curly braces.
ARRAY LENGTH
• All arrays store the allocated size in a
variable named length.
• To access the length of the array a using
a.length.
• Each dimension of the array is indexed from
zero to its maximum size minus one.
• Example:

int aSize = a . Length;


TWO-DIMENSIONAL ARRAYS

• In this, the first index selects the row and the second index
selects the column within that row.
• For creating two-dimensional array, same steps are to be
followed as that of simple arrays.
• Example:
int myArray[ ][ ];
myArray = new int [3] [4];
Collections in java
Difference between array and arraylist
Exception handling
• Java Exceptions
• When executing Java code, different errors can occur:
coding errors made by the programmer, errors due to
wrong input, or other unforeseeable things.
• When an error occurs, Java will normally stop and
generate an error message. The technical term for this
is: Java will throw an exception (throw an error).
Java try and catch

• The try statement allows you to define a block of code to


be tested for errors while it is being executed.

• The catch statement allows you to define a block of code


to be executed, if an error occurs in the try block.

• The try and catch keywords come in pairs:

You might also like