[Draft] Training Guide ‐ OOP in Java - vinhtbkit/bkit-kb GitHub Wiki

OOP (Object-Oriented Programming) is also known as OOPs (Object-Oriented Programming System).
In Java programming, the OOP is an important concept.
Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

Target: the trainee will understand the OOP in Java and can apply it to convert the real-world object to a class in Java.
Expected Duration: maximum 24 hours of training and working on exercises.

Qualification criteria:

  • The trainee must understand the OOP concepts in Java: Class, Object, Inheritance, Polymorphism, Abstraction, Encapsulation.
  • Knowledge about the pillars of OOPs.
  • Modifiers in Java and their scopes.

What is OOP? REQUIRED:

References:

OOPs in Java:

References:

Class REQUIRED:

References:

Object REQUIRED:

References:

Constructors REQUIRED:

References:

Attributes REQUIRED:

References:

Methods REQUIRED:

References:

Modifiers REQUIRED:

References:

Inheritance REQUIRED:

References:

Polymorphism REQUIRED:

  • Compile-time Polymorphism
  • Runtime Polymorphism

References:

Abstraction REQUIRED:

References:

Encapsulation REQUIRED:

References:

Quizzes:

  • What are the pillars of the OOP concept? Explain the meaning of each pillar.
  • Why Do We Need Java Inheritance? And How to Use Inheritance in Java?
  • Why Do We Need Java Abstraction? Distinguish Encapsulation vs Abstraction. Difference between Interface class vs Abstract class in Java.
  • What are method overloading and method override? Differences.
  • How many constructors a class can have?
  • If we do not define a modifier for a method, attribute,... What is a default? And its scopes?
  • Why Java not is a purely object-oriented language?
  • Could you explain the difference between OOP and POP (Procedural Oriented Programming) and Object-based programming?
  • What is the purpose of getters and setters in a class? Why don't we just use public fields?

Assignments:

Requires:

Knowledge about OOPs in Java, Java Syntax

Exercise:
We have three shapes: Circle, Rectangle, and Square. Each object type has general information below:

  • color
  • is filled or not

FYI, to calculate the area and/or perimeter for:

  • Circle: we need a radius
  • Rectangle: we need a width and height
  • Square: we need a side

Requirements:

  • Write a Java program that model above shapes. You should be using Interface and/or Abstract Class to model the Shape.
  • Each Shape implementation should contain methods to get area, perimeter
  • The color and filled attribute should make sense to each other. Ex: filled should be true when color is set, if color is null filled should be false.
  • Write a toString method to display the shape information: color, filled, radius/width-height/side
  • Then write some tests by yourself to check the working of these classes:
  • Write utility methods to get the shape having max area from an array of shapes

Example:

Shape s1 = new Circle(3.5, "red", false);  
System.out.println(s1);                    
System.out.println(s1.getArea());          
System.out.println(s1.getPerimeter());     
System.out.println(s1.getColor());
System.out.println(s1.isFilled()); // expect should be true
System.out.println(s1.toString()); // Example format: "Circle: {color: red, filled: true, radius: 3.5}"
   
Circle c1 = (Circle) s1; // cast to Circle                  
System.out.println(c1);
System.out.println(c1.getArea());
System.out.println(c1.getPerimeter());
System.out.println(c1.getColor());
System.out.println(c1.isFilled());
System.out.println(c1.getRadius());

// Question:
Shape s2 = (Shape) c1; // Can I cast a circle to a Shape? Why?