Class in Java

Java is an object-oriented programming language

What is object-oriented Programming?

Here is an excerpt from a 1994 Rolling Stone interview where Apple co-founder Steve Jobs explains what object-oriented programming is

Objects are like people. They're living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we're doing right here.

Here's an example: If I'm your laundry object, you can give me your dirty clothes and send me a message that says, "Can you get my clothes laundered, please." I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, "Here are your clean clothes."

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can't even hail a taxi. You can't pay for one, you don't have dollars in your pocket. Yet I knew how to do all of that. And you didn't have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That's what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.

Why use object-oriented Programming?

Object-oriented programming provides a clear and modular structure for creating programs.

 

It is easy to maintain and modify existing code using the class structure.

Class in Java

1. Classes act as templates for the creation of objects, specifying their state and behavior

2. An object created by a class is called an instance of that class

3. A class is itself an object, and an instance of the class "Class" or a "blueprint" for creating objects

Creating a class in Java

public class Person {
  // Just an empty class,
  // we don't create anything yet here
}

To create a class, use the keyword class

Create an Object

public class Person {
    String name = "Bob";

    public static void main(String[] args) {
        // create object of person
        Person person = new Person();
        System.out.println(person.name);
    }
}

To create an object of Person, specify the class name, followed by the object name, and use the keyword new

Attributes

Java Class Attributes

In the previous chapter, we used the term variable for name in the example (as shown below). It is actually an attribute of the class. Or you could say that class attributes are variables within a class:

public class Person {
    String name = "Bob";
    int  = 20;
}

Accessing Attributes

You can access attributes by creating an object of the class, and by using the dot syntax (.)

public class Person {
    String name = "Bob";

    public static void main(String[] args) {
        // create object of person
        Person person = new Person();
        System.out.println(person.name);
    }
}

Modify Attributes

You can access attributes by creating an object of the class, and by using the dot syntax (.)

public class Person {
    String name = "Bob";

    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);

        person.name = "Nur Ratna";
        System.out.println(person.name);
    }
}

Methods

Agains?!

Constructors

Java Constructor

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes

public class Person {
    String name;

    // Create a class constructor for the MyClass class
    public Person() {
        // Set the initial value for the class attribute name
        name = "Bob"; 
    }

    public static void main(String[] args) {
        // Set the initial value for the class attribute name
        Person person = new Person();
        System.out.println(person.name);
    }
}

Constructor Parameter

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes

public class Person {
    String name;

    public Person(firstName, lastName) {
        name = firstName + " " + lastName; 
    }

    public static void main(String[] args) {
        Person person = new Person("Nur", "Ratna");
        System.out.println(person.name);
    }
}

Class in Java

By Nur Ratna Sari

Class in Java

  • 39