• Latest
  • Trending
Java and Object Oriented Programming

Java and Object Oriented Programming

June 13, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Monday, 1 June, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

Java and Object Oriented Programming

by ITECHNEWS
June 13, 2022
in Data Science, Leading Stories
0 0
0
Java and Object Oriented Programming

What is Object Oriented Programming(OOP)?

This is a programming paradigm based on the concept of objects, which can contain data and code which data is in the form of fields also known as attributes or properties and code in the form of procedures also known as methods.

Many of the most widely used programming languages such as C++, Javascript and Python namely but a few implement Object Oriented Programming other than Java.

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

What are Objects?

Objects are always called instances of a class and has a state and behavior. For example, a dog is an object that has

  • State: name, breed, color etc
  • Behaviors: barking, running, sleeping etc

Class

A class in java is a blueprint for the object and before we create an object we first need to define the class.

Think of the class a sketch(prototype) of a house and it contains all details about the windows, floors, doors and the others. Based on these descriptions we build the house and therefor the House is the object.

NB: Many different houses can be made based on the same description of the class.

Creating a class in Java

We can create a class in Java using the class keyword. Such as

class className {
    // fields
    // nethods
}

In this, the fields and methods represent the state and behavior of the object respectively.

  • fields are used to store data
  • methods are used to perform some operations

Our car object we can create the class

class Car {
    // state or field
    String name;
    int manufacturer;
    String color;

    // behavior or method
    void braking() {
        System.out.println("Car is braking");
   }

   void speeding() {
        System.out.println("Car is speeding");
   }
}

  • In the above example, we have created a class named Car which has fields name, manufacturer, color and methods braking(), speeding().
  • Car is a prototype and now we can create any number of cars using the prototype. All the bicycles will share the fields and methods of the prototype.

Java Objects

An object is an instance of a class. Our class we created called Car then sportsCar, suvCar and f1Car can be considered as the objects of that class.

Creating an Object in Java

This is how an object of a class is created in java

className objectName = new className();

// for Car class
Car sportsCar = new Car();

Car suvCar = new Car();

// sportsCar and suvCar are the names of the objects.

Let’s observe the use of the new keyword along with the constructor of the class to create an object. Constructors are almost the same as methods and use the same name as the class only that it doesn’t have a return type. for example;

class Car {
    Car() {
    // constructor body
 }
}

// Car() is a constructor and doesn't have a return type

Access Members of a class

The name of the object is along with . (the dot operator) we can be able to access the members of a class. For example;

class Car {
    // state or field
    String name;
    int manufacturer;
    String color;

    // behavior or method
    void braking() {
        System.out.println("Car is braking");
   }

   void speeding() {
        System.out.println("Car is speeding");
   }
}

// create object named suvCar
Car suvCar = new Car();

// access fields
suvCar.name;
suvCar.maufacturer;

// access methods
suvCar.speeding();

Here you can be able to access the fields and methods of this class.

We are now able to understand what a class is and an object. Let’s look at a fully working example;

class Lamp {

  // stores the value for light
  // true if light is on
  // false if light is off
  boolean isOn;

  // method to turn on the light
  void turnOn() {
    isOn = true;
    System.out.println("Light on? " + isOn);

  }

  // method to turnoff the light
  void turnOff() {
    isOn = false;
    System.out.println("Light on? " + isOn);
  }
}

class Main {
  public static void main(String[] args) {

    // create objects led and halogen
    Lamp led = new Lamp();
    Lamp halogen = new Lamp();

    // turn on the light by
    // calling method turnOn()
    led.turnOn();

    // turn off the light by
    // calling method turnOff()
    halogen.turnOff();
  }
}

Output:

Light on? true
Light on? false

In our next part we are going to look at methods, overloading and constructors.
Later we shall look into concepts like abstraction, encapsulation, inheritance and polymorphism.

Source: Benjamin Rukundo
Tags: Java and Object Oriented Programming
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version