Object-oriented concepts of java

Published: Last Edited:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

Class

A class describes the data and the methods of its object. It cleanly shows the state and behavior of its object. It also has its own data (class variables) and class methods, every class has one and only one immediate superclass and the super class has one immediate superclass until it reaches it's a object.

Example:

public class Bicycle {

public int cadence;

public int gear;

public int speed;

public Bicycle(int startCadence, int startSpeed, int startGear) {

gear = startGear;

cadence = startCadence;

speed = startSpeed;

}

public void setCadence(int newValue) {

cadence = newValue;

}

public void setGear(int newValue) {

gear = newValue;

}

public void applyBrake(int decrement) {

speed -= decrement;

}

public void speedUp(int increment) {

speed += increment;

}

}

Object

An object is a software bundle of related state and behavior. Object always comes under a class. Objects are key to understanding object oriented technology.

Example:

public class search

{

int ArraySize;

int [] Element;

int seqsearch( int Target) throws SearchException

{

int index;

for( index=0; index<ArraySize;index++)

{

if( Element[index]==Target)

else

throw new SearchException("Target not found.");

}

}

Constructor

A constructor is a method which is created automatically when an object is created. Constructors allow you to specify stating values for properties, and other such initialization details.

Example:

public class Rectangle {

public int width = 0;

public int height = 0;

public Point origin;

public Rectangle() {

origin = new Point(0, 0);

}

public Rectangle(Point p) {

origin = p;

}

public Rectangle(int w, int h) {

origin = new Point(0, 0);

width = w;

height = h;

}

public Rectangle(Point p, int w, int h) {

origin = p;

width = w;

height = h;

}

Inheritance

In heritance provides a powerfull and natural mechanism for organizing and structuring your software. This section explains how classes inherit state and behavior from their superclasses. A sub class also can inherit fields and method from a superclass.

Example:

public class Bicycle {

public int cadence;

public int gear;

public int speed;

public Bicycle(int startCadence, int startSpeed, int startGear) {

gear = startGear;

cadence = startCadence;

speed = startSpeed;

}

public void setCadence(int newValue) {

cadence = newValue;

}

public void setGear(int newValue) {

gear = newValue;

}

public void applyBrake(int decrement) {

speed -= decrement;

}

public void speedUp(int increment) {

speed += increment;

}

}

Interfaces

A interface is a reference type, similar to a class, that can contain only constant method signatures, and nested type. There are no method bodies. Interface can not be instantiated they can only be implemented by classes or extended by other Interfaces.

Example:

public interface Laughable

{

public void laugh();

}

public interface Booable

{

public void boo();

}

Encapsulation

Means that the internal representioon of an object is separated form the interface it presents to ather objects in your program in other words a programmer that uses your class only needs to know what the public methods do not how they work the advantage is that you can change how your class works to improve performana or add new features without breaking any code that relies on the methods provided by your original class.

Example:

Public class Baboo

{

Public int size;

Public int weight;

}

Public class ExploitBaboo

{

Public static void main(String [] args)

{

Badoo b=new Badoo();

b.size=5;

}

}

Polymoephism

The two kids of polymorphism he overloding and overding. Overloding occurs when a class declares two or more methods with the same name but different signatures when amessage is sent to an object or class with overloaded methods the method with the best matching signature is the one that is used (involed)

Example:

Class Adder

{

Public int addThem(int x, int y)

{

Return x+y;

}

Public double addThem(double x, double y)

{

Return x+y;

}

}

Overrding

Overrding occurs when aclass declares a methods with the same signature as that of an in herited methods when a message is sent to the object or class if it's a class method the locally defined method is the one that is used.

Example:

Class Animal

{

Public void eat() throws Exception

{

}

Class Dog2 extends Animal

{

Public void eat()

{

}

Public static void main(String [] arg)

{

Animal a=new Dog2();

Dog2 d=new dog2();

d.eat();

a.eat();

}

}