You are on page 1of 25

Object Oriented Programming concepts

Encapsulation
Abstraction
Access Modifiers

Dr B.Veera Jyothi
Assistant Professor
IT Department,CBIT.
FEATURES OF OBJECT ORIENTED PROGRAMMING

 Objects

 Classes

 Data Abstraction

 Encapsulation

 Inheritance

 Polymorphism
FEATURES OF OBJECT ORIENTED PROGRAMMING

 Objects:

Objects are Basic Runtime Entities.


 Classes:

 Class is a Collection of Objects.


class cbitece
{
---
}
How do you create an object in Java?
cbitece student=new cbitece();
FEATURES OF OBJECT ORIENTED PROG
FIRST JAVA PROGRAM

Set Path:

Java Compilation:
javac filename.java
Java Execution:
java filename
class Test class System
{ {
static String S=“CBIT”; static Printstream out;
} }

Test.S.length(); System.out.println();

 Java.lang package in Java


Provides classes that are fundamental to the design of the
Java programming language.
Example:
System,Wrapper Class.
class cbitIT
{
void Read()
{
System.out.println(“IT IS BEST”);
}
void Write()
{
System.out.println(“IT IS BEST”);
}
}
Class cbitdemo
{
public static void main(String args[])
{
cbitIT student=new cbitIT();
student.Read();
Student.Write();
}
}
FEATURES OF OBJECT ORIENTED PROGRAMMING

 Data Abstraction

Data Abstraction means providing only essential information


to the outside world without including Background details or
Explanations.
Abstraction can achieved in two ways:
a) Abstract Class
b) Interface

 Encapsulation

Encapsulation is a mechanism where you bind


your data and code together as a single unit.
ABSTRACTION IN JAVA
FEATURES OF OBJECT ORIENTED PROGRAMMING

Encapsulation can achieved in Java by:


 Declaring the variables of a class as private.

 Providing public setter and getter methods to modify and


view the variables values.
// JAVA PROGRAM TO DEMONSTRATE ENCAPSULATION
class Encapsulate {
// private variables declared these can only be accessed by public methods of
class
private String Name;
private int Roll;
private int Age;

// get method for age to access private variable Age


public int getAge() { return Age; }

// get method for name to access private variable Name


public String getName() { return Name; }

// get method for roll to access private variable Roll


public int getRoll() { return Roll; }
// set method for age to access private variable age
public void setAge(int newAge) {Age = newAge; }

// set method for name to access private variable Name


public void setName(String newName)
{
Name = newName;
}
// set method for roll to access private variable Roll
public void setRoll(int newRoll) {Roll = newRoll; }
}
public class TestEncapsulation {
public static void main(String[] args)
{
Encapsulate obj = new Encapsulate();

// setting values of the variables


obj.setName("Harsh");
obj.setAge(19);
obj.setRoll(51);

// Displaying values of the variables


System.out.println("name: " + obj.getName());
System.out.println(" age: " + obj.getAge());
System.out.println("roll: " + obj.getRoll());

// Direct access of Roll is not possible


// due to encapsulation
// System.out.println("roll: " +
// obj. Name);
}}
PROGRAMMING EXERCISE
 Write a program to create a room class,
 the attributes of this class is
 roomno, roomtype, roomarea and ACmachine.
 In this class the member functions are
 setdata and displaydata.
FEATURES OF OBJECT ORIENTED PROGRAMMING

 Inheritance
Classes can share, obtain or “inherit” properties and
methods that belong to existing classes. 
FEATURES OF OBJECT ORIENTED PROGRAMMING
1.Which of these is correct way of inheriting class A by
class B?
a) class B + class A {}
b) class B inherits class A {}
c) class B extends A {}
d) class B extends class A {}

2.Does Java support multiple level inheritance?


a) True
b) False
POLYMORPHISM

 Polymorphism means taking many forms, where ‘poly’


means many and ‘morph’ means actions.

.
4 PILLARS OF OOPS
WHAT IS THE OUTPUT OF THE
FOLLOWING PROGRAM
class Test {
  
    // Declaring and initializing integer variable
    int x = 10;
  
    // Main driver method
    public static void main(String[] args)
    {
          // Creating an object of class inside main()
        Test t = new Test();
  
        // Printing the value inside the object by above created object
        System.out.println(t.x);
    }
}
ACCESS IN PACKAGES
P1 P2
Protection Protection2

Derived OtherPackage

SamePackage
Demo1 Demo
ACCESS IN PACKAGES
ACCESS IN PACKAGES

You might also like