OOPs | Object Oriented Design Programming Using Java - Part 1

Object-Oriented Programming is a paradigm that provides many concepts, such as inheritance, data binding, polymorphism, etc. 

Simula is considered the first object-oriented programming language. The programming paradigm where everything is represented as an object is known as a truly object-oriented programming language.

Smalltalk is considered the first truly object-oriented programming language. 

The popular object-oriented languages are Java, C#, PHP, Python, C++, etc. 

The main aim of object-oriented programming is to implement real-world entities, for example, object, classes, abstraction, inheritance, polymorphism, etc. 

OOPs (Object-Oriented Programming System) Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object- Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts:
  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation
Apart from these concepts, there are some other terms which are used in Object-Oriented design:
  • Coupling
  • Cohesion
  • Association
  • Aggregation
  • Composition

Object
Any entity that has state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.

An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other's data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects. 

Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.

object orient program

Class
Collection of objects is called class. It is a logical entity. 

A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space.

Inheritance
When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

Polymorphism
If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.

In Java, we use method overloading and method overriding to achieve polymorphism.

Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.

Abstraction
Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't know the internal processing.

In Java, we use abstract class and interface to achieve abstraction.
Encapsulation
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is wrapped with different medicines.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.

Coupling
Coupling refers to the knowledge or information or dependency of another class. It arises when classes are aware of each other. If a class has the details information of another class, there is strong coupling. In Java, we use private, protected, and public modifiers to display the visibility level of a class, method, and field. You can use interfaces for the weaker coupling because there is no concrete implementation.

Cohesion
Cohesion refers to the level of a component which performs a single well-defined task. A single well-defined task is done by a highly cohesive method. The weakly cohesive method will split the task into separate parts. The java.io package is a highly cohesive package because it has I/O related classes and interface. However, the java.util package is a weakly cohesive package because it has unrelated classes and interfaces.

Association
Association represents the relationship between the objects. Here, one object can be associated with one object or many objects. There can be four types of association between the objects:
  • One to One
  • One to Many
  • Many to One, and
  • Many to Many
Let's understand the relationship with real-time examples. For example, one country can have one prime minister (one to one), and a prime minister can have many ministers (one to many). Also, many MP's can have one prime minister (many to one), and many ministers can have many departments (many to many).

Association can be undirectional or bidirectional.


Aggregation
Aggregation is a way to achieve Association. Aggregation represents the relationship where one object contains other objects as a part of its state. It represents the weak relationship between objects. It is also termed as a has-a relationship in Java. Like, inheritance represents the is a relationship. It is another way to reuse objects.

Composition
The composition is also a way to achieve Association. The composition represents the relationship where one object contains other objects as a part of its state. There is a strong relationship between the containing object and the dependent object. It is the state where containing objects do not have an independent existence. If you delete the parent object, all the child objects will be deleted automatically.


Java Metaclass
Class members
Class variables are shared by all instances of a class. Declaring a variable in a class to be "static" achieves this.

Static variables can be initialized with a static block in the class. This code executes when the class is first loaded, after the initialization (if any) of class vars.

Book says that static variables are "global", but encapsulation modifiers still apply. Also, the name of the variable must be preceeded by its class name, so name collision problems don't
arise.

Class variables that are made final serve as C-like constants, since there value can't be changed by subclasses. The convention is to name them with ALL CAPS.

Class methods are declared static as well. They may not refer to ivars, this, or super.

Class methods are executed by messaging the class itself with the dot operator.

Class Class
Every class and interface has a class object to represent it. The class of Class is used to represent the types of classes and interfaces (metaclass).

Things that Class does for you:
  • discover, manipulate class space
  • find out which classes are loaded, which interfaces are conformed to
  • load new classes from disk or the network
  • instantiate an object of a class whose name is provided at run time
Example: Discovering type info
This method from Class returns the Class object for a Type identified by the String name.

Class forName(String name)

Once the Class object is known for a particular class you can ask it for the following:

boolean isInterface()
Class[] getInterfaces()
Class getSuperclass()

Example: Creating objects from their name 

You can find the metaclass for a particular class, as above, then use newInstance() to create an object of that class

Class metaClass = forName("BubbleSort");
BubbleSort sortObj = (BubbleSort) metaClass.newInstance();

But good is this neat trick if you need to know the type you're expecting to do the right typecast, and they type for a reference to access the object with? Consider the SortHarness/BubbleSort
problem, and a generic main method:

public static void main(String[] args)
{
try {
Class metaClass = Class.forName(args[0]);
SortHarness sortObj = (SortHarness)metaClass.newInstance();
sortObj.sort(testData);
// print results
} catch (Exception e) {
System.err.print(e);
}
}

Now we can test any sorting algorithm by running main with the command line argument of the name of the sorting class.


The base abstract class serves as a means of referring generically to any object which is a subclass of SortHarness. Another way (probably better) is to use an interface to describe objects
which may be loaded and messaged. 

Example: Loading a class unconventionally 

It is possible to overload the method which is used to automatically load classes as they are referenced. This method is part of the ClassLoader class

protected abstract Class loadClass(String name, boolean resolve) throws ClassNotFoundException

If you extend ClassLoader to load from a special place, then you must

  • make sure the class is only loaded once (keep track of what you've loaded)
  • check if a system class is being loaded, then use that definition
  • read the bytes in the class file for a non-system class that hasn't been loaded from wherever is appropriate
  • resolve the class if requested
<game player example where games come via email>

Post a Comment

0 Comments