Object-oriented programming is at the core of Java. Now what is this Object-orientedness?
Let's start by discussing what is an object:--
In real world, anything that you can percieve is an object. Real-world objects share two characteristics. They all have state and behaviour.
e.g. Dogs have state(colour,breed) and behaviour(barking,wagging tail).
Cars have state(colour, size,gear,brakes) and behaviour(changing gear, applying brakes).
Similarly in software-world, software objects are similar to real-world objects.; they too consist of state and related behaviour. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages).
Going back into programming world:-
All computer programs consists of two elements. code and data. Furthermore, a program can be conceptually organized around its data. An object-oriented program can be characterized as data controlling access to code.
Abstraction:-
An essential element of object-oriented programming is abstraction. You might be guessing what is this abstraction?
Let's demonstrate this with the help of an example.
e.g. A Car. People do not think of a car as a set of tens of thousands of individual parts. They think of it as a well defined object with its unique behaviour. They can ignore the details of how the engine ,transmission, and braking work. Instead, they are free to utilize the object as a whole.
Abstraction is thus the process or result of generalization by reducing the information content of a concept or an observable phenomenon, typically in order to retain only information which is relevant for a particular purpose.
The Three OOP Principles:-
Let's start by discussing what is an object:--
In real world, anything that you can percieve is an object. Real-world objects share two characteristics. They all have state and behaviour.
e.g. Dogs have state(colour,breed) and behaviour(barking,wagging tail).
Cars have state(colour, size,gear,brakes) and behaviour(changing gear, applying brakes).
Similarly in software-world, software objects are similar to real-world objects.; they too consist of state and related behaviour. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages).
Going back into programming world:-
All computer programs consists of two elements. code and data. Furthermore, a program can be conceptually organized around its data. An object-oriented program can be characterized as data controlling access to code.
What is a Class?
Classes are the fundamental building blocks of a Java program. In real world, you will find many individual objects of the same kind. For example, there may be hundreds of cars in existence, with same design and model. They may contain the same components. In object-oriented terms we say that your car is an instance of the class of objects known as cars.
In Java, we define a class in the following way,
class Cars {
int model;
int price;
.............
int model;
int price;
.............
.............
//Contains some other data members and methods
}
We shall discuss more about classes and objects when we do some programming.
A Few Terminologies:-
Abstraction:-
An essential element of object-oriented programming is abstraction. You might be guessing what is this abstraction?
Let's demonstrate this with the help of an example.
e.g. A Car. People do not think of a car as a set of tens of thousands of individual parts. They think of it as a well defined object with its unique behaviour. They can ignore the details of how the engine ,transmission, and braking work. Instead, they are free to utilize the object as a whole.
Abstraction is thus the process or result of generalization by reducing the information content of a concept or an observable phenomenon, typically in order to retain only information which is relevant for a particular purpose.
The Three OOP Principles:-
- Encapsulation-- Encapsulation is the mechanism that binds together code and the data that it manipulates, and keeps them safe from both outside interference and misuse. You can imagine encapsulation as a protective wrapper which prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. The code and data inside the wrapper can only be accessed through a well-defined interface.
- Inheritance-- Inheritance is the process by which one object acquires the properties of another object. Examples of inheritance can be derived from real life itself. A Golden Retriever is a part of the classification of dog, which in turn is part of a mammal class, which again is under larger class animal. By use of inheritance, an object need only define those qualities that makes it unique within the class. It can inherit its general attributes from its parent. Object-oriented programming allows classes to inherit commonly used state and behaviour from other classes.
![]() |
Inheritance |
The syntax for inheritance is simple. At the beginning of your class declaration use the extends keyword
followed by the name of the class to inherit from.
class Mercedes Car extends Cars {
// Some code goes here.....
}
followed by the name of the class to inherit from.
class Mercedes Car extends Cars {
// Some code goes here.....
}
- Polymorphism-- Polymorphism (from Greek, meaning "many forms") is a feature that allows one interface to be used for a general class of actions. In other words, polymorphism refers to the ability to appear in many forms. Polymorphism allows you define one interface and have multiple implementation.
public class Dog extends Animal{
public void makeSound(){
System.out.println("Woof!");
}
public void makeSound(boolean injured){
System.out.println("Whimper!");
}
}
At this point you may not understand the above code entirely as we have not dealt with Java coding as yet. But the code says that " a dog which is a type of animal can make sound which are of two types". First one is the normal barking sound that a dog makes while the other is a sound which a dog make when it is injured. Thus, sound is produced by a dog in several forms.
Next we shall learn How a Java Code is compiled.
_____________________________________________________________________________________
No comments:
Post a Comment