📜 ⬆️ ⬇️

Why does it seem to me that students are learning OOP wrong

When I studied at the university, it was quite hard for me to understand OOP (Object-Oriented Programming), now I understand that we were just taught OOP with not quite clear and correct analogies and in general, it seems, the teachers themselves did not quite understand what the essence of OOP .

image

Remember, the classic analogies of the PLO, there is a class “Pets” with the methods “voice” and “is”, from it we inherit the Cat and the Dog and everything is fine.

But here comes the Light and brings aquarium fish that do not talk, and then comes Vasya, who brings a favorite cactus, which not only does not talk, but does not eat.
')
We are already confused, but Little Johnny asks: “Where are the static methods, interfaces, abstract classes in this zoo and how does a class object differ from a class itself?”. It is certainly possible to explain, but difficult. Understand, even more difficult.

Or another classic example, here is a rectangle, from which one wants to inherit a square (well, logically, a square is a special case of a rectangle), but a rectangle has a length and width, and a square has only one side. Something is confusing here too.

Now we will think how to explain OOP better?

Object-oriented programming is not a description of objects in the real world (more precisely, not only and so much a description of the real world), it appeared from design technicians (before the advent of any computers) from all engineering bureaus, building architects, equipment designers, etc.

image

So, imagine an aircraft manufacturing company and a design office at this company (well, let's say, Boeing). We ordered several models: military aircraft, cargo and passenger.

The first thing we will do is start drawing some general drawing for all models. According to this drawing, it is still impossible to build a real aircraft, but it already contains general implementation details. This is an abstract class.

Java code
public abstract class  {  //     ;
        public abstract void ();
        public void () {
            //     
        }
        public abstract void ();
 }


, , , , ( , ), , , . .

Java
public interface  { //      
        long get();
        double get();
        long get();
        void ();
        void ();
        void ();
}

public abstract class  implements  {
    ...


, , , , (/)   .

Java
public abstract class  {  //     ;
        public abstract void (); //  
        public void () {
            //     
        }
        public abstract void (); //  
 }


, . . , , . , , . .

Java
public class  extends  {  
       public int get() {
              return 120;
        }
        public void () {
            __();
            _();
            ___();
            ():
        }
        ...
}
public class   extends  {  
       public int get() {
              return 5;
        }
        public void () {
            _();
            _();
            ():
        }
        ...
}


() . , , , . () .

Java
  = new ();

.set(0.99);


image

, , . . .

Java
.set(0.0);


2 , , (, ) , ( , ). .

Java
public class  extends  {  
       ...
        public void () {
            _();
            ___();
            ___();
            ():
        }
        ...
}
public class   extends  {  
        public void () {
            _();
            _();
            ():
        }
        ...
}


, , ( ), « - ». .

Java
public class  extends  {  
       private   = new 45()
        ...
}


, (, , ), . .

120 , - 130 ( ), , . , - 120 , 130 , , , . .

Java
public class 130 extends  {  
       @Override
        public int get() {
              return 130;
        }
        ...
}


?

, , . , . , - (, , ), - () .

, , , . . .

( ), , , , - (, , , ), , . — . — ..

!

P.S. (, Java SE)

Source: https://habr.com/ru/post/345658/


All Articles