You are on page 1of 9

Object Oriented Programming

Assignment 02

Name: V.T.Gowthaman
Reg No: ITT/2015/2016/017
Index No: 0182
Data Hiding
Data hiding is a software development technique specifically used in object-
oriented programming (OOP) 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.

Eg:
public class Hiding {
private int a;
private String b;
public void setA(int a) {
this.a = a;
}
public void setB(String b) {
this.b = b;
}
public static void main(String[] args) {
// TODO code application logic here
}

}
Abstraction
Abstraction is the process of abstraction in Java which is used to hide certain
details and only show the essential features of the object. In other words, it deals
with the outside view of an object (interface).

Eg:
public class Account {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}
class gautham extends Account{

}
Encapsulation
Java - Encapsulation. Advertisements. Encapsulation is one of the four
fundamental OOP concepts. The other three are inheritance, polymorphism, and
abstraction. Encapsulation in Java is a mechanism of wrapping the data (variables)
and code acting on the data (methods) together as a single unit.
Eg:
class Encapsulatio {
private int x;
private String y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
}
public class Encap{
public static void main(String[] args) {
// TODO code application logic here
Encap Encap = new Encap();
}}
Tightly Encapsulated Class
A class is said to be tightly encapsulated if and only if, all the data members
declared as private. ... If a class is having only private variables and no other public
variables then a class is said to be tightly encapsulated class. It does not depend
on the getter or setter methods.

Eg:
public class Employee {
private String name;
public String GetName() {
return name;
}

public void SetName(String name) {


this.name = name;
}
public static void main(String[] args) {
}
}
Is-A Relationship
In Object oriented programming, IS-A relationship denotes “one object is type of
another”. IS-A relation denotes Inheritance methodology. In Java, Inheritance can
be implemented with extends (in case of class) and implements (in case of
interface) keywords.

Eg:
public class vehicle {
}
public class FourWheeler extends Vehicle {
}
public class TwoWheeler extends Vehicle {
}
public class Car extends FourWheeler {
}
Has-A Relationship
Has-A means an instance of one class “has a” reference to an instance of another
class or another instance of same class. There is no specific keyword to implement
HAS-A relationship but mostly we are depended upon “new” keyword.

Eg:
public class HasARelatioship {
public static void main(String[] args) {
b B = new b();
a A = new a();
B.name();
A.name();
}
}
class a extends HasARelatioship{
public void name(){
System.out.println("this is a class");
}
}
class b extends a{
public void name(){
System.out.println("this is class B");
}}
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.

Eg:
public class Main {
public static void main(String[] args) {
b B = new b();
a A = new a();
B.name();
A.name();
}
}
class a extends Main{
public void name(){
System.out.println("this is a class");
}
}
class b extends a{
public void name(){
System.out.println("this is class B");
}}
Overriding
The benefit of overriding is, ability to define a behavior that's specific to the
subclass type, which means a subclass can implement a parent class method
based on its requirement. In object-oriented terms, overriding means
to override the functionality of an existing method.

Eg:
class Dog{
public void bark() {
System.out.println(“woof”);
}
}
class Hound extends Dog{
public void sniff() {
System.out.println(“sniff “);
}
public void bark() {
System.out.println(“bowl”);
}
}

You might also like