Course Content‎ > ‎Session 1‎ > ‎

Intro to Objects

Object-Oriented Concepts

This is also referred to as OOP. In a lot of ways, OOP conceptualizes a software development, in ways similar to a construction project. Typically in a construction project, a blueprint defines how several objects would be laid out and fit with each other. Similarly, in OOP, you work with defining a blueprint of each object and put them together to end up with a software program.

What is an Object?

An Object may be defined as an entity that has a set of defined attributes and method(s). Attributes relate to the properties of an object and methods depict the behavior of a given object. The smallest unit of OOP world would be an object. 

Attributes of an object are individual entities that differentiate one class of objects from another. In a class attributes are typically defined as variables. Variables are of two types:

Instance variable or Object Variable represents the attributes of one object.

Class variable defines an attribute of the entire class. This variable applies to the entire class.

Behavior of Objects
A set of methods may be used to define, what a class of objects can do to other objects or to themselves. Changing the attributes help change the state of an object and hence, define its behavior. Like Mood = angry or Mood = sad or Mood = Happy.

Method Definition:
A group of related statements in a class of objects that act on themselves or on other classes or objects. They are very similar to functions. 

Classes

A class is a blueprint that is used to create multiple objects with similar features. When defining objects in Java or any other OOP programming language, you don't define objects, but you define a class that has objects. When the program runs, objects are created from those classes and used as needed.

Class Library 

A class library is a group of classes, designed to be used, with other programs. Java ships with a predefined set of libraries. Additional set of libraries may be written for large applications.

Building Classes

Let's start with the basic definition:

class human
{

}

Let's create some instance variables:

String hungry;
String name;

Let's define a method that defines some behavior:

void feedhuman()

{

        if (hungry == true)

        {

            System.out.println("I am hungry");

            hungry = false;

        }

        else  

        {

            System.out.println("I am full.");

                      }  

}

Let's define another method to output some data:

void showAttributes()
{
System.out.println("My name is " + name);

if (hungry == true)
    System.out.println(name + " is hungry.");
else
    System.out.println(name + " is full.");
}

So, now we have a class with two instance variables and two methods. Every java program should have a main method in which we will create an instance of the class human and operate on it.
 
    public static void main (String arguments[])
    {
        human j = new human();
        j.name = "John";
        j.hungry = true;
      
        System.out.println("Calling showAttributes ...");
        j.showAttributes();
        System.out.println("-----");  
        System.out.println("Feeding John ...");
        j.feedhuman();
        System.out.println("-----");  
        System.out.println("Calling showAttributes ...");
        j.showAttributes();  
        System.out.println("-----");
        System.out.println("Feeding John ...");
        j.feedhuman();
    }
 
So this what the human class would look like:
 
    class human
    {
        String name;
        boolean hungry;
        void feedhuman()
        {
            if (hungry == true)  
            {
                System.out.println("I am hungry");
                hungry = false;
            }
            else  
            {
                System.out.println("I am full.");
            }  
        }
        void showAttributes()
        {
            System.out.println("My name is " + name);
            if (hungry == true)  
               System.out.println(name + " is hungry.");
            else
               System.out.println(name + " is full.");
        }
        public static void main (String arguments[])
        {
            human j = new human();
            j.color = "orange";
            j.sex = "male";
            j.hungry = true;
            j.showAttributes();
            System.out.println("-----");
            j.feedhuman();
            System.out.println("-----");
            j.showAttributes();
            System.out.println("-----");
            j.feedhuman();
        }
    }

Class Organization

Lot of time interrelated classes can impact on other classes by enforcing their attributes on other classes. Inheritance is one such concept. Inheritance enables one class to inherit all of the behavior and attributes of the other class. With inheritance, all classes are arranged in a strict hierarchy. A class that inherits attributes from another class is called a subclass and the class that gives the inheritance is a superclass. A class can have only one superclass. A superclass can have several subclasses. Subclasses inherit attributes from the superclass. Subclassing is creating a new class from an existing class and modifying attributes (if necessary) and methods.

When defining class hierarchy, it requires a lot of planning and revision. The goal is to reduce the number of repetitive features that might be needed. Once an attribute is defined, it can be inherited automatically by the superclass. 

When a new object is created, Java keeps track of every variable that is defined for the objects and each variable that is defined in its superclass. In this way all classes combine to form a template for a current object. Methods work in a similar scope. New objects have access to all method names of its class and superclass(es). This is determined dynamically, when a method is used in a running program. When a call to a method is made, the interpreter checks the object class. If it can't find it there, it looks at the superclass one at a time till it finds it.  If a duplicate method is defined, then the method that is hit first (starting from the bottom of the hierarchy), is the one that is used. For this to happen, the method in a given class must have the same name, return type and arguments as the super class. This procedure is called overriding.

Single and Multiple Inheritance

Single inheritance is a mechanism in which a subclass can only have one superclass. In multiple inheritance, an object can have multiple superclasses, which leads to complex class definitions. Such definitions are not allowed in Java. Java solves this problem by using interfaces.Interface is a collection of methods that indicate a class has some behavior in addition to what it inherits from its superclass.

Packages  

Packages group related classes and interfaces.  A class library in java is called a package.
  1. By default, your Java classes have access to classes in java.lang.
  2. To use any other classes, you must refer to them implicitly
  3. To refer to a class within a package, you must use a period.

For example, color class is in the awt package in the javalibrary. So to refer to the color class, you use the following:

java.awt.color
Comments