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

Java OOP Concepts Explained

The document provides an overview of Object-Oriented Programming (OOP) concepts, including classes, objects, encapsulation, abstraction, inheritance, and polymorphism. It explains key features such as static variables and methods, constructors, method overloading, and method overriding, along with examples. Additionally, it discusses abstract classes and interfaces, highlighting their roles in achieving polymorphism and structuring code.

Uploaded by

sherarup512
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 views62 pages

Java OOP Concepts Explained

The document provides an overview of Object-Oriented Programming (OOP) concepts, including classes, objects, encapsulation, abstraction, inheritance, and polymorphism. It explains key features such as static variables and methods, constructors, method overloading, and method overriding, along with examples. Additionally, it discusses abstract classes and interfaces, highlighting their roles in achieving polymorphism and structuring code.

Uploaded by

sherarup512
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

OOP Concept

OOP(Object Oriented Programming) Concept


Created by: Alan Kay
First OOP based programming: Simula
First famous OOP based programming: C++
Java’s OOP comes from C++
Java is more Object Oriented and Secure than C++
Java is C++ without the guns, clubs and knives.
(by- James Gosling)

[ ©copyright reserved by incapp ] [Link] JAVA


Class and Object
Class is a blueprint of the object.
Object is a physical entity.

[ ©copyright reserved by incapp ] [Link] JAVA


Class and Object

[ ©copyright reserved by incapp ] [Link] JAVA


OOP’s Main Concepts
Encapsulation
Abstraction
Inheritance
Polymorphism

[ ©copyright reserved by incapp ] [Link] JAVA


Encapsulation
Binding/Wrapping the component together to make them secure
is known as Encapsulation.

[ ©copyright reserved by incapp ] [Link] JAVA


Abstraction
Hiding the complex working(Complexity) by providing the
functionality is known as Abstraction.

[ ©copyright reserved by incapp ] [Link] JAVA


Inheritance
Using the old design to creating new design is known as
Inheritance.

[ ©copyright reserved by incapp ] [Link] JAVA


Polymorphism
Poly means “Many” and Morphism means “Forms”.

Many forms of doing a job.


Examples- 1) Display has many forms. 2) Fly has many forms.

[ ©copyright reserved by incapp ] [Link] JAVA


Object Anatomy

class Employee{
String name;
int salary;
String cname;
}

[ ©copyright reserved by incapp ] [Link] JAVA


Object Diagram
class Employee{
name
String name;
int salary; null cname
String cname; a salary null
}
0

Employee a=new Employee();


Employee b=new Employee(); name
null cname
b salary null
0

[ ©copyright reserved by incapp ] [Link] JAVA


Static Keyword

static keyword can be used inside the class as:


• Static variable
• Static method
• Static block
• Static nested class

[ ©copyright reserved by incapp ] [Link] JAVA


Static Variable
A common member for every object of that class.

[ ©copyright reserved by incapp ] [Link] JAVA


Static Variable
• Can be accessed via Class-Name and Object reference.
• Allocated when the class loads in memory.

class Employee{
String name;
int salary;
static String cname;
}

[ ©copyright reserved by incapp ] [Link] JAVA


Static Method
• Can be accessed via Class-Name and Object reference.
• Allocated when the class loads in memory.
• Can access static member but can not access non-static
member of that class.

class A{
static void methodName(){
//body
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Static Block
• Automatically executes when the class loads in memory.
• Only executes once.
• Can access static member but can not access non-static
member of that class.
• Mostly used for initializing the static variable.

class A{
static {
//body
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Static vs Non-Static Member
Static member Non-Static member
• Also known as Class Member. • Also known as Instance Member.
• Common for Every Object of the • Individual for Every Object of the
class. class.
• Can be accessed via Class name • Can be accessed via Object
or Object reference. reference only.
• Allocated when class loads in • Allocated when object created in
memory. memory.
• Static members can be accessed • Non-Static members can be
from static and non-static accessed from a non-static context
context both. only.

[ ©copyright reserved by incapp ] [Link] JAVA


Constructor
A special method in a class that is executed automatically
when an object of the class created.

class Employee{
Employee(){
[Link](“Object Created”);
}
}

Employee a=new Employee();


Employee b=new Employee();
Employee c=new Employee();

[ ©copyright reserved by incapp ] [Link] JAVA


Constructor vs Method
Constructor Method
• Special method in a class. • Normal method in a class.
• Name must be same as class • Name is the choice of
name. programmer.
• Can not declare return type • Can declare return type or
(even, void not allowed). void.
• Invoked implicitly with • Invoked explicitly by
object construction. programmer’s choice.
• Mostly used to initialize the • Used to do anything that
object of the class. programmer needs.

[ ©copyright reserved by incapp ] [Link] JAVA


Types of Constructor
Non-Parameterized Parameterized
class Employee{ class Employee{
Employee(){ Employee(String name){
//body //body
} }
} }

[ ©copyright reserved by incapp ] [Link] JAVA


Constructor Overloading
class Employee{
Employee(){
//body
}
Employee(String name){
//body
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Constructor Chaining
class Employee{
Employee(){
//body
}
Employee(String name){
this();
//body
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Constructor Chaining Rules
• Can not chain more than one constructor in a constructor.
• Constructor chaining statement must be the first statement.
• Recursive constructor chaining is not allowed.
• “this” and “super” constructor chaining can not be
used together in a constructor.

[ ©copyright reserved by incapp ] [Link] JAVA


Initialize Block
• A special block in a class that is executed automatically
before constructor when an object of the class created.
• This block having no name that’s why also known as
anonymous block of the class.

class Employee{
Employee(){
//body
}
{
//body
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Inheritance
Mechanism of creating new class from old one is called Inheritance.
class Human{
//members Super/Parent/Base class
}

class Student extends Human{


//members Sub/Child/Derived class
}

[ ©copyright reserved by incapp ] [Link] JAVA


Types of Inheritance

In Java classes Multiple Inheritance is NOT supported. But in Java Interfaces it is supported.

[ ©copyright reserved by incapp ] [Link] JAVA


Polymorphism
Poly means “Many” and Morphism means “Forms”.
In Real World:
Many forms of doing a job.
Examples- 1) Display has many forms. 2) Fly has many forms.

In Programming:
Many methods having same name.

[ ©copyright reserved by incapp ] [Link] JAVA


Types of Polymorphism

Polymorphism

Compile-Time(Static) Run-Time(Dynamic)
Polymorphism Polymorphism

Achieved by Achieved by
Method Overloading Method Overriding

[ ©copyright reserved by incapp ] [Link] JAVA


Compile-Time Polymorphism
Having more than one method with same name and different arguments in
a class is known as Compile-Time polymorphism / Static Polymorphism.

class Addition{
void add(int x,int y){
In compile-time polymorphism,
//body
the compiler binds the method
}
call to the method declaration
void add(String x,String y){
at compile time, and the JVM
//body
executes it as bound.
}
}

Compile-Time Polymorphism = Method Overloading

[ ©copyright reserved by incapp ] [Link] JAVA


Method Overloading
Having more than one method with same name and different
arguments in a class is known as Method Overloading.

Rules for method overloading:


• Method name must be same.
• Method argument must be different.
• Method return type can be same or different.
• Inheritance is optional.
• Method can be static or non-static.

[ ©copyright reserved by incapp ] [Link] JAVA


Run-Time Polymorphism
Having same signature non-static methods in super and
sub classes, and calling the sub class method from the
super-class reference, then we achieve Run-Time
polymorphism.

Run-Time Polymorphism = Method Overriding + Non-primitive Upcasting

[ ©copyright reserved by incapp ] [Link] JAVA


Method Overriding

Having same signature non-static methods in super


and sub classes is known as Method Overriding.

Rules for method overriding:


• Method name must be same.
• Method argument must be same.
• Method return type must be same.
• Inheritance is compulsory.
• Access modifier must be same or strong.
• Method must be non-static.

[ ©copyright reserved by incapp ] [Link] JAVA


Method Hiding

Having same signature static method in super and sub


classes is known as Method Hiding.

Rules for method hiding:


• Method name must be same.
• Method argument must be same.
• Method return type must be same.
• Inheritance is compulsory.
• Access modifier must be same or strong.
• Method must be static.

[ ©copyright reserved by incapp ] [Link] JAVA


Method Overriding & Hiding Table

Defining a Method in Sub-class with Super-class Instance Super-class


the Same Signature as in Super-class (Non-Static) Method Static Method

Sub-class Instance
Overrides error
(Non-Static) Method

Sub-class Static Method error Hides

[ ©copyright reserved by incapp ] [Link] JAVA


Data Hiding

Having same name variable in super and sub classes is


known as Data Hiding.

Rules for data hiding:


• Variable name must be same.
• Variable type can be same or different.
• Inheritance is compulsory.
• Access modifier can be same or different.
• Variable can be static or non-static.

[ ©copyright reserved by incapp ] [Link] JAVA


Type Casting
Non-Primitive Type-Casting Primitive Type-Casting
class A{ class B extends A{ int x=10;
void m1(){ } void m2(){ } double y=x; [ upcasting ]
} } int z=y ; [ compile error ]
int z=(int)y; [ downcasting ]
B x=new B();
A y=x; [ upcasting ]
B z=y ; [ compile error ]
B z=(B)y; [ downcasting ]

A y=new B() ; [ upcasting ]


B x=new A() ; [ compile error ]
B x=(B)new A() ; [ ClassCastException ]

[ ©copyright reserved by incapp ] [Link] JAVA


Without Run-Time Polymorphism
public class Circle{
private double a; public class ShapeApp {
public void area(){ public static void main(String [] s){
a=3.14 X 5 X 5 ; Circle c=new Circle();
} [Link]();
public void show(){ [Link]();
SOP(“Circle’s Area: ”+a) ; Rectangle r=new Rectangle();
} r.m1();
} r.m2();
Triangle t=new Triangle();
public class Rectangle{ [Link]();
private double v; [Link]();
public void m1 (){ }
v=10 X 7 ; }
}
public void m2(){
SOP(“Rectangle’s Area: ”+v) ;
}
}

public class Triangle{


private double ar;
public void calculate(){
a= 15 X 8 / 2;
}
public void display(){
SOP(“Triangle’s Area: ”+ar) ;
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


With Run-Time Polymorphism
public class Circle extends Shape{ public class Shape{
private double a; public void findArea(){
public void findArea(){
a=3.14 X 5 X 5 ; }
} public void printArea(){
public void printArea(){
SOP(“Circle’s Area: ”+a) ; }
} }
}

public class Rectangle extends Shape{


private double v; public class ShapeApp {
public void findArea(){ public static void main(String [] s){
v=10 X 7 ; Shape s;
} s=new Circle();
public void printArea(){ s. findArea();
SOP(“Rectangle’s Area: ”+v) ; s. printArea();
} s=new Rectangle();
} s. findArea();
s. printArea();
public class Triangle extends Shape{ s=new Triangle();
private double ar; s. findArea();
public void findArea(){ s. printArea();
a= 15 X 8 / 2; }
} }
public void printArea(){
SOP(“Triangle’s Area: ”+ar) ;
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Abstract Class
public class Circle extends Shape{
abstract public class Shape{
private double a;
public void findArea(){
public void findArea(){
a=3.14 X 5 X 5 ;
}
}
public void printArea(){
public void printArea(){
SOP(“Circle’s Area: ”+a) ;
}
}
}
}

public class Rectangle extends Shape{


private double v; public class ShapeApp {
public void findArea(){ public static void main(String [] s){
v=10 X 7 ; Shape s= new Shape() ;
} s=new Circle();
public void printArea(){ s. findArea();
SOP(“Rectangle’s Area: ”+v) ; s. printArea();
} s=new Rectangle();
} s. findArea();
s. printArea();
public class Triangle extends Shape{ s=new Triangle();
private double ar; s. findArea();
public void findArea(){ s. printArea();
a= 15 X 8 / 2; }
} }
public void printArea(){
SOP(“Triangle’s Area: ”+ar) ;
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Abstract Class
• A class that can not be instantiated.
• A class that is used to access its sub-classes only.
• A class that is used to achieve Run-time polymorphism.

[ ©copyright reserved by incapp ] [Link] JAVA


Abstract Method
public class Circle extends Shape{
private double a;
abstract public class Shape{
public void findArea(){ abstract public void findArea() ;
a=3.14 X 5 X 5 ; abstract public void printArea() ;
} }
public void printArea(){
SOP(“Circle’s Area: ”+a) ;
}
}

public class Rectangle extends Shape{


private double v; public class ShapeApp {
public void findArea(){ public static void main(String [] s){
v=10 X 7 ; Shape s= new Shape() ;
} s=new Circle();
public void printArea(){ s. findArea();
SOP(“Rectangle’s Area: ”+v) ; s. printArea();
} s=new Rectangle();
} s. findArea();
s. printArea();
public class Triangle extends Shape{ s=new Triangle();
private double ar; s. findArea();
public void findArea(){ s. printArea();
a= 15 X 8 / 2; }
} }
public void printArea(){
SOP(“Triangle’s Area: ”+ar) ;
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Abstract Method
• A method without body.
• A method that must be overridden in its
non-abstract(Concrete) sub-class.

[ ©copyright reserved by incapp ] [Link] JAVA


Interface
public class A {
public void show(){ abstract public interface Shape{
SOP(“Hello A”) ;
abstract public void findArea() ;
}
abstract public void printArea() ;
}
}

public class Rectangle extends A implements Shape{


private double v;
public void findArea(){ public class Test{
v=10 X 7 ; PSVM(String [] s){
} Shape s= new Shape() ;
public void printArea(){ s=new Circle();
SOP(“Rectangle’s Area: ”+v) ; s. findArea();
show(); s. printArea();
} s=new Rectangle();
} s. findArea();
s. printArea();
public class Triangle implements Shape{ s=new Triangle();
private double ar; s. findArea();
public void findArea(){ s. printArea();
a= 15 X 8 / 2; }
} }
public void printArea(){
SOP(“Triangle’s Area: ”+ar) ;
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Interface vs Abstract Class
Interface Abstract class
• It is 100% abstract class by default. • It can be 100% abstract or not.
• Supports multiple inheritance. • Does not supports multiple inheritance.
• Does not support constructor. • Supports constructor.
• Method in interface is by default • Method in abstract class is not by
abstract and public. default abstract and public.
• Variable in interface is by default • Variable in abstract class is not by
public, static and final. default public, static and final.
• To inherit an interface in class, we use • To inherit an abstract class, we use
implements keyword and To inherit an extends keyword.
interface in interface, we use extends
keyword.
• From Java-8, default method and static
method are allowed in the interface.
• From Java-9, private method is also
allowed in the interface.

[ ©copyright reserved by incapp ] [Link] JAVA


Final keyword
• Final class (A class that can not be Inherited)
• Final method (A method that can not be overridden or hidden)
• Final variable (A variable, who's value can not be changed)
final class final variable
final class A{ class A{
//members final int x=10;
} void method(){
final int z=20;
final method }
class A{ }
final void method(){
//body
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Blank Final Variable
class Test{ class Test{ class Test{
final int a; final int a; static final int a;
Allowed Test(){ { static {
a=10; a=10; a=10;
} } }
} } }

class Test{ class Test{ class Test{


final int a; static final int a; final int a;
Test(){ Test(){ static{
a=10; a=10; a=10;
Not Allowed } } }
{ } }
a=10;
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Nested Class
• Non-static nested class
• Static nested class
• Local nested class
• Anonymous nested class

[ ©copyright reserved by incapp ] [Link] JAVA


Non-Static Nested Class
class A{ class Demo{
void m1(){ PSVM(-){
B b=new B(); A a=new A();
SOP(b.z); A.B b=[Link] B();
b.m(); SOP(b.z);
} b.m();
class B{ }
int z; }
void m(){
SOP(“hi B”);
}
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Static Nested Class
class A{ class Demo{
void m1(){ PSVM(-){
B b=new B(); A.B b=new A.B();
SOP(b.z); SOP(b.z);
b.m(); b.m();
} }
static class B{ }
int z;
void m(){
SOP(“hi B”);
}
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Local Nested Class
class A{ class Demo{
void m1(){ PSVM(-){
class B{ A a=new A();
int z; a.m1();
void m(){ }
SOP(“hi B”); }
}
}
B b=new B();
SOP(b.z);
b.m();
}
void m2(){
// B b=new B(); //error
}
}
[ ©copyright reserved by incapp ] [Link] JAVA
Anonymous Nested Class
class A{
void m1(){
SOP( “Hi A”);
}
}

class Demo{
PSVM(-){
A a=new A() {
void m1(){
SOP( “Hi INCAPP”);
}
};
a.m1();
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Lambda Expression
Introduced in Java 8
Lambda expression provides implementation of functional interface.
Functional Interface
An interface which has only one abstract method is called functional interface.
Java Lambda Expression Syntax: (argument-list) -> {body}

Special Interfaces

Marker Interface Functional Interface


Interface that has Interface that has
no abstract method only one abstract method

Lambda expression works with Functional Interface only.

[ ©copyright reserved by incapp ] [Link] JAVA


Lambda Expression
@FunctionalInterface //It is optional
interface Addition{
public void add();
}

Implementing above interface in two ways:

Without Lambda With Lambda


// using anonymous class Addition a= ( ) -> {
Addition a=new Addition(){ int a=10,b=20,r=a+b;
public void add(){ [Link](“Sum : ”+r);
int a=10,b=20,r=a+b; };
[Link](“Sum : ”+r); [Link]();
}
};
[Link]();

[ ©copyright reserved by incapp ] [Link] JAVA


Lambda Expression
Note:
If lambda expression body contains only one statement then curly braces are optional.
Addition a=()->[Link](“Sum : ”+(10+20));

Lambda Expression with Parameter


@FunctionalInterface
interface Addition{
public void add(int x, int y);
}
public class LambdaExp {
public static void main(String[] args) {
Addition a=(x, y)->{
int r=x+y;
[Link](“Sum : ”+r);
};
[Link](10,20);
}
}
Note: Data-type in argument is optional.

[ ©copyright reserved by incapp ] [Link] JAVA


Lambda Expression
Note:
If lambda argument-list contains only one argument then round braces is optional.
@FunctionalInterface
interface Square{
public void findSquare(int x);
}
public class LambdaExp {
public static void main(String[] args) {
Square s= x -> {
int r=x*x;
[Link](“Square : ”+r);
};
[Link](10);
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Lambda Expression
Lambda Expression with Return-type and Parameter
@FunctionalInterface
interface Addition{
public int add(int x, int y);
}
public class LambdaExp {
public static void main(String[] args) {
Addition a=(x, y)->{
return x+y;
};
[Link]( “Sum : ”+[Link](10,20) );

//also Allowed without return keyword


Addition a=(x, y)-> x+y;
[Link]( “Sum : ”+[Link](10,20) );
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Package
• Used to avoid the naming confliction of byte codes in a
project
• Used to categorized the byte codes.

[ ©copyright reserved by incapp ] [Link] JAVA


Access Modifiers
Most Accessible
public: accessible all over the enclosing project.
protected: accessible inside the enclosing package and
inside the sub-class of any package of the enclosing
project.
Default(package private): accessible only inside the
enclosing package .
private: accessible inside the enclosing class.
Less Accessible

[ ©copyright reserved by incapp ] [Link] JAVA


protected keyword
Protected member is accessible inside the enclosing package and inside
the sub-class of any package of the enclosing project.
package p1
package p2
public class A{
import p1.A; protected int x=10;
public class B{ }
public void m(){
A a= new A(); public class B{
SOP(a.x); //error public void m(){
} A a= new A();
} SOP(a.x);
}
import p1.A; }
public class C extends A{
public void m(){ public class C extends A{
SOP(x); public void m(){
} SOP(x);
} }
}

[ ©copyright reserved by incapp ] [Link] JAVA


Sub-Packages
Package inside the package known as Sub-Package or Nested Package.

package p1
import p2.B; // Error
package p3 import p1.p2.B;
public class A{
import p2.B; // Error public void m(){
import p1.p2.B; B b=new B();
public class D{ SOP( b.y );
public void m(){ }
B b=new B(); }
SOP( b.y );
} package p1.p2
}
public class B{
public int y=20;
}

[ ©copyright reserved by incapp ] [Link] JAVA


import static
• import static is used to directly import static-member of the class.
• Introduced in java 5.0
package p1

package p2 public class A{


public int x=10;
static public int y=20;
import static p1.A.y;
public void m1(){
import static p1.A.m2;
//body
public class D{
}
PSVM(-){
static public void m2(){
m2();
//body
SOP( y );
}
}
}
}

[ ©copyright reserved by incapp ] [Link] JAVA


Rules for class
• Only one class can be public in a java program.
• If a class is public in a java program then Java program
name must be same as name of that public class.

[ ©copyright reserved by incapp ] [Link] JAVA


Access Modifiers

[ ©copyright reserved by incapp ] [Link] JAVA

You might also like