Monday, April 9, 2018

What are Object Oriented Programming Practices




Java is what is known as an Object-Oriented Language. This means that it specializes with objects. As such, it provides numerous programming concepts such as:
  • Java Objects
  • Java Classes
  • Instance
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation



In this lesson, we’ll deal mostly with Objects and Classes; what they are in java, how they are created, how to use them, and what they are used for.




Are there any other forms of programming languages besides the OOPs?

Before the development of Object Oriented programming languages and concepts, the majority of programming was done with procedural or procedure-oriented programming languages. Languages that utilised these concepts included C and C++;

How are OOPs better than the Procedural?

The OOPs have a lot of advantages over the Procedural languages, some of which are as follows:
  • Development and maintenance of code is easier in OOPs than in Procedural, because in OOPs, code is arranged in objects and classes, making it easier to manage code if the project grows.
  • OOPs also provide you with the ability to easily simulate real-world events more effectively.
  • OOPs allow you to hide your data with the use of access modifiers (as treated in the previous tutorial), whereas Procedural languages contain no access modifiers, and so can be access from anywhere.
Java Objects

An object in java can be considered as an object in the real-world sense. In our environment, we can find objects around us, both living and non-living. Examples include dogs, pens, tables, plants… even humans can be considered as objects. A common characteristic of all these things I’ve mentioned is that they possess two things; state and behaviour.

Therefore, an object is java is anything that has a state and a behaviour.


Let us consider a dog for example. Its state includes its name, colour, breed, and even number of spots on its body. Its behaviour could include running, barking, tail wagging, and even tricks it can do.

If we created a dog object in java, for example; it’s state would be stored in variables (String breed, int age). It’s behaviour, on the other hand, would be shown in methods.

Therefore, in software development, most interaction between objects is done via methods.
Read also : Methods in java 
Java Classes

A class is a collection of objects. It can also be considered as a blueprint where individual objects are created.
The following is an example of a class called Dog, showing its various states and behaviours:

public class Dog {

 String name;
 int age;
 String breed;

 void barking() {

 }

 void running() {

 }

 void tricks() {

 }

}

Class Constructors
A constructor, as the name implies, is a piece of code that creates or constructs an object. Every class has its own constructor. In fact, if a class is not explicitly created, the Java compiler will automatically build a default constructor for said class.
Therefore, a constructor is invoked anytime a new object is created in a class. A constructor must have the same name as the class. A class can also have more than one constructor.

public class Dog {

 String name;
 int age;
 String breed;
 
 public Dog() {
  
 }
 
 public Dog(String name) {
//  This is constructor 
 }
}

Rules for creating java constructor:
There are basically two rules defined for the constructor:
1. Constructor name must be same as its class name
2. Constructor must have no explicit return type

Types of java constructors:
There are two types of constructors in java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor

1. Default constructor:

A constructor is called "Default Constructor" when it doesn't have any parameter.

Syntax:
<class_name>()  //Default constructor
{
//body
}

Example:
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.

 class Bike{
  Bike()  //Default constructor 
  {
   System.out.println("Speedy"); //It will print Speedy
  }
  public static void main(String args[])
  {
   Bike a=new Bike(); //instance of a class
  }
 }

Creating Objects in Java
  • In Java, there are three steps to take when creating an object from a class:
  • Declaration: Declaring the variable with a name and an object type.
  • Instantiation: Using the ‘new’ keyword to create the object
  • Initialization: Following the keyword with a call to the constructor.
Now for this example, I created a project called Animal, with a main class with the same name. I then went to the Projects tab at the side window and right clicked the Animal package. I clicked on Java class and named it Dog in the dialog box.
Now, here is an example of how to create an object:
public class Animal{
 
 public static void main(String[] args) {
  Dog dog = new Dog("Dough");
 }
}
So based on the above code, we can say that I created an animal. A dog to specific, whose name is Doug.
Now, say I want to print out the name of my dog. All I have to do is go back to the Dog constructor and insert this code:
public class Dog {

 String name;
 int age;
 String breed;
 
 public Dog() {
  
 }
 
 public Dog(String name) {
//  This is constructor 
          this.name = name;
 }
}
Now, let us print out the age and breed of our dear Doug. First, I’ll have to go back to our constructor and add some more parameters and ‘this’ keywords:


public class Dog {

 String name;
 int age;
 String breed;
 
 public Dog() {
  
 }
 
 public Dog(String name ,int age ,String breed) {
//  This is constructor 
          this.name = name;
          this.age =age;
          this.breed =breed;
 }
}
We’ll then have to go back to our main class and make a few minor additions:
That is enough on Classes for now. Next lesson, we’ll talk on other Object Oriented Programming Concepts.
Other Tutorials posts:


  • Making Games With Java- Introduction
  • Getting Everything you need for Java
  • Introduction to Java Programming
  • Basic data types and Variables in java
  • Basic Data types and Variables in java - Continued...
  • User Input using Scanner class in java
  • Conditional Logic in java
  • Loops in java
  • Arrays in java
  • Wrapper classes in java
  • Methods in java
  • If you have any issue leave us a comment .

    Thanks for reading
    noeik

    0 comments:

    Post a Comment