Sei sulla pagina 1di 10

OBJECT ORIENTED

PROGRAMMING
Assignment- 2

S.Arjeen

ITT/2015/2016/002

0169
Discussion Topic set 1

Data Hiding

Data hiding is a software development technique specifically used in object-


oriented programming to hide internal object details (data members). Data
hiding ensures exclusive data access to class members and protects object
integrity by preventing unintended or intended changes.

public class CheckingAccount {

private double balance = 0;

public void setBalance(double bal) {


balance = bal;
}

public double getBalance() {


return balance;
}
public static void main(String[] args){

}
}
Abstraction
Object-oriented programming, abstraction is a process of hiding the
implementation details from the user, only the functionality will be provided to
the user. In other words, the user will have the information on what the object
does instead of how it does it.

public class Abstraction{


public static void main(String args[]){
TwoWheeler test = new Honda();
test.run();
}
}
abstract class TwoWheeler {
public abstract void run();
}
class Honda extends TwoWheeler{
public void run(){
System.out.println("Running..");
}
}
Encapsulation

Encapsulation is one of the four fundamental OOP concepts. Encapsulation in Java


is a mechanism of wrapping the data (variables) and code acting on the data
(methods) together as a single unit. In encapsulation, the variables of a class will be
hidden from the classes and can be accessed only through the methods of their
current class.

class Student {

private String name;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public class Test {

public static void main(String[] args) {


Student s = new Student();
s.setName("vijay");
System.out.println(s.getName());
}
Tightly Encapsulated Class

A tightly encapsulated class does not allow public access to any data member that
can be changed in any way.so encapsulation helps to protect internal data from the
possibility of corruption from external influences.

public class A {
private int x;
public static void main(String[] args){
}
}
class B {
private int x;
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
}
IS-A Relationship
In object-oriented programming, the concept of IS-A is a totally based on Inheritance,
which can be of two types Class Inheritance or Interface Inheritance. It is just like saying
"A is a B type of thing". For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is
uni-directional. For example, House is a Building. But Building is not a House.

public class IsARelationship {

float salary = 30000;

class Science extends IsARelationship

float bonous = 2000;

public static void main(String args[]) {

Science obj = new Science();

System.out.println("Salary is:" + obj.salary);

System.out.println("Bonous is:" + obj.bonous);

}
HAS-A Relationship
In Has-A relationship an object of one class is created as data member in
another class the relationship between these two classes is Has-A. In Has-A
relationship there existed physical memory space and it is also known as part
of or kind of relationship.

public class HasARell {

private String color;

private int maxSpeed;

public void carInfo(){

System.out.println("color" + color + "max speed"+ maxSpeed);

public void setColor(String color) {

this.color = color;

public void setMaxSpeed(int maxSpeed) {

this.maxSpeed = maxSpeed;

public static void main(String[] args) {

}
Overloading
Method Overloading is a feature that allows a class to have more than one method
having the same name, if their argument lists are different. It is similar to constructor
overloading in Java, that allows a class to have more than one constructor having
different argument lists.

public class OverLoaing


{
public void callByValue(int x)
{
x=100;
}
public static void main(String[] args)
{
int x=50; OverLoaing t = new OverLoaing();
t.callByValue(x);
System.out.println(x);
}
}
Overriding

Overriding is an object-oriented programming feature that enables a child class to


provide different implementation for a method that is already defined and/or
implemented in its parent class or one of its parent classes. The overridden method
in the child class should have the same name, signature, and parameters as the one
in its parent class

class Overriding {

void run() {
System.out.println("Vehicle is running");
}
}

public class Bike extends Overriding {

public static void main(String args[]) {


Bike obj = new Bike();
obj.run();

}
}

Potrebbero piacerti anche