2.4.6
Encapsulation & Polymorphism
Encapsulation
Encapsulation
Encapsulation in object-oriented programming is a principle that concerns how data inside the class can be accessed or changed.
Setters and getters
Setters and getters
- A setter is a special type of method that sets one of the attributes equal to a specific value.
- A getter is a special type of method that obtains the value of one of the attributes of the object.
Encapsulation
Encapsulation
- Each attribute can only be accessed or modified by methods that are set (encapsulated) when defining the class.
- This is also known as 'information hiding'.
- The use of these methods protects the data in the class instance from being tampered with.
Different instances
Different instances
- An instance of a class should not be able to alter the data of another instance of the same class.
- For example, if you have an instance of a car class called “car1”, its methods should not be able to alter the attributes of another instance of the car class that is also running.
Polymorphism
Polymorphism
In object-oriented programming, polymorphism is when a subclass alters its inherited methods in two ways: by overloading or overriding.
Overloading
Overloading
- Overloading is when a method is defined in the subclass that has the same name, but different arguments to a method defined in the superclass.
- The method might also have a different return type.
- For example, a shape superclass might have an area calculating function.
- The subclass circle could have the function area(radius), whereas a triangle subclass could have the function area(base,height).
Overriding
Overriding
- Overriding is when a method is redefined in the subclass with the same name and same arguments.
- The method that is defined in the subclass overrides the method defined in the super class.
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
Jump to other topics
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
Unlock your full potential with GoStudent tutoring
Affordable 1:1 tutoring from the comfort of your home
Tutors are matched to your specific learning needs
30+ school subjects covered