Skip to content

Advanced OOP

Higher level object oriented programming

  • Lesson
  • 3 min read
  • 4 of 9 in this chapter

What are the different concepts of OOP

There are 4 different basic concepts that go into object oriented programming. These different concepts help us create an object in our code that helps it easily function with the rest of the code. What are these different concepts? They are encapsulation, inheritance, abstraction, and polymorphism.

Encapsulation

Encapsulation is the act of hiding data of an object from the outside code. For example…

Java
class encapsulationExampleClass{
 int integerValue = 1;
 //two parts of encapsulation are the getter and setter methods
 //a getter will get a value from the class
 //a set will set a value in a class
 int getIntegerValue(){
 return integerValue;
 //this will return the value of integerValue to the user
 }
 void setIntegerValue(int value){
 integerValue = value;
 /*this sets the integerValue to the
 method parameter integer given to it*/ 
 }
}
public class OopConcepts {
 public static void main(String[] args) {
 encapsulationExampleClass example = new encapsulationExampleClass();
 System.out.println(example.getIntegerValue());
 //this will print out the value 1
 example.setIntegerValue(5);
 //this will set the 'integerValue' to 5
 System.out.println(example.getIntegerValue());
 //this will print out integerValue "5"
 }
}

Inheritance

Inheritance is when a one class derives its qualities from another class. For example if we have a ‘person’ class and a ‘banker’ class the ‘banker’ class inherits qualities from the ‘person’ class.’

Java
class vehicle{
	//this is a parent or "super" class
	//we can have methods and attributes in our parent class
	int wheels = 4;
	void park(){
		System.out.println("A vehicle can park");
	}
}
class bus extends vehicle{
	//this is a sub-class, which inherits vehicle
	/*"extends" tells us that a bus gets the basic
	 attributes and methods of a vehicle*/
	int seats = 60;
	void busStop(){
		System.out.println("a bus stops at a bus stop");
	}
}
class suv extends vehicle{
	//we can have multiple sub-classes
	int seatCapacity = 4;
}

public class OopConcepts {
 public static void main(String[] args) {
 	suv ourSuv = new suv();
 	bus ourBus = new bus();
 	/*this is how we declare our sub-classes
 	 in the main method*/
		System.out.println(ourBus.wheels);
		//we can get our attributes of the sub and super class
		ourBus.busStop();
		//we can use methods like usual
		ourSuv.park();
		/*the sub-classes objects can access methods
		 from the super class*/
 }
}

Polymorphism

Polymorphism is where one object or method can be implemented in different ways. For example, if there are two animal classes ‘bird’ and ‘dog’ and both objects have a ‘move’ method(command) applied to them, the ‘move’ command will be applied differently to each object. A dog will move by walking but a bird will move by flying.

For example...

Java
class vehicle{
 //they both inherit common traits
 int wheels = 4;
}
//both classes have the same method
//they both state their utility
//but both methods state and do different things
class truck extends vehicle{
 void utility(){
 System.out.println("A truck is for carrying things");
 }
}
class sportscar extends vehicle{
 void utility(){
 System.out.println("Sports cars are used to go fast");
 }
}
//this is polymorphism

Abstraction

Abstraction is how data that an object contains is completely hidden from another object or user. For example, if there is a ‘human’ class and a ‘car’ class, to drive the car the human only needs to know how to operate the steering wheel, brakes, accelerator, etc. The human does not need to know how to operate the datatypes of a radiator, transmission, engine, etc. Thus all of that data is hidden or abstracted from the human.