Jul 262017

ADM

Java OOP Tutorials

 

 

Java OOP Tutorials - java

 

To understand OOP (Object-oriented Programming) we need to compare with real-life example. Just look around and you will see that each object can be seen as composed of multiple objects. E.g. a car: engine, chassis, wheels, brakes, etc.

The same as in real life, objects will interact within a running application.

What Is a Class?

A class is a source code blueprint for objects. That blueprint specifies each object's behavior and state variables and how will interact with other objects. Compared with our real-life example, will be all the informations and processes needed to create on component. e.g. all info how to create the engine.

What Is an Object?

An object is a bundle of variables and related methods. In other words, an object is an instance of a class. Compared with our real-life example the object will the car component. e.g. the engine itself.

Concepts

There are four main concepts that define the OOP:

  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Inheritance

Different kinds of objects often have a certain amount in common with each other. Object-oriented programming allows classes to inherit commonly used state and behaviour from other classes. Compared with our real-life example: there are multiple types of cars: SUV, trucks, buses, etc. and all of this can have similar components.

Translating this into java will have Car.java class that describe all the common behaviour and then will have a class for each type of car that will inherit the generic class: BusCar.java, TruckCar.java,SUVCar.java.

Note: In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses.

Polymorphism

The idea of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. In programming the polymorphism is in association with inheritance and abstraction. To understand polymorphism please follow the next example.

public class Person {
	
	public void sayHello() {
		System.out.println("Hello");
	}	
}


public class Student extends Person {
	
	public void sayHello() {
		System.out.println("Hello Student!");
	}
}


public class Teacher extends Person {
	
	public void sayHello() {
		System.out.println("Hello Teacher!");
	}
}

And here is the test class

public class PolyTest {
	public static void main(String[] args)
	{
		Person st = new Student();
		Person te = new Teacher();
		
		st.sayHello();
		te.sayHello();
	}
}

The output for this is

Hello Student!
Hello Teacher!

As you can see the text printed belongs to each child class.

Abstraction

Abstraction is the process of hiding the implementation details and showing only the functionality. In Java, you can obtain this by using abstract classes and interfaces.

Encapsulation

Encapsulation is the process of wrapping code and data together into a single unit or class. This code will be not available outside of the class. Also, the class will provide methods to handle the communication with other objects. This was will have full control of the object behavior.

 

OOP Tutorials

References