The document covers basic Object-Oriented Programming (OOP) concepts, including classes, objects, inheritance, polymorphism, and encapsulation. It explains how objects represent real-world entities and how classes serve as templates for creating these objects. Additionally, it discusses the importance of encapsulation for data hiding and the role of abstraction in hiding implementation details from users.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
30 views18 pages
Week 6 Basic OOP Concepts
The document covers basic Object-Oriented Programming (OOP) concepts, including classes, objects, inheritance, polymorphism, and encapsulation. It explains how objects represent real-world entities and how classes serve as templates for creating these objects. Additionally, it discusses the importance of encapsulation for data hiding and the role of abstraction in hiding implementation details from users.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18
Week 6
Basic OOP Concept
CCPRGG2L – Intermediate Programming LEARNING OBJECTIVES At the end of lesson students should be able to: • Understand classes and objects in programming. • Know inheritance and Polymorphism. • Apply basic OOP concept in program, which will enable you to develop GUI and large-scale software systems effectively. Objects and Classes Defining Classes for Objects Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. Objects and Classes Defining Classes for Objects An object has a unique identity, state, and behavior. • The state of an object (also known as its properties or attributes) is represented by data fields with their current values. A circle object, for example, has a data field radius, which is the property that characterizes a circle. A rectangle object has data fields width and height, which are the properties that characterize a rectangle. • The behavior of an object (also known as its actions) is defined by methods. To invoke a method on an object is to ask the object to perform an action. For example, you may define a method named getArea() for circle objects. A circle object may invoke getArea() to return its area. Objects and Classes Defining Classes for Objects • A class is a template, blueprint, or contract that defines what an object’s data fields and methods will be. • An object is an instance of a class. You can create many instances of a class. • Creating an instance is referred to as instantiation. The terms object and instance are often interchangeable. Objects and Classes Displaying GUI Components • Graphical user interface (GUI) components are good examples for teaching OOP. • When you develop programs to create graphical user interfaces, you will use Java classes such as JFrame, JButton, JRadioButton, JComboBox, and JList to create frames, buttons, radio buttons, combo boxes, lists, and so on. Objects and Classes Displaying GUI Components Objects and Classes Displaying GUI Components
You can add graphical user
interface components, such as buttons, labels, text fields, check boxes, and combo boxes to the window. The components are defined using classes. Encapsulation Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding. Encapsulation To achieve encapsulation in Java: • Declare the variables of a class as private. • Provide public setter and getter methods to modify and view the variables values. Encapsulation To achieve encapsulation in Java: • Declare the variables of a class as private. • Provide public setter and getter methods to modify and view the variables values. Inheritance • Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order. • The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class). Inheritance • extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword. Abstraction • Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it. In Java programming, abstraction is achieved using Abstract classes and interfaces. Abstraction A Java class which contains the abstract keyword in its declaration is known as abstract class. • Java abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); ) • But, if a class has at least one abstract method, then the class must be declared abstract. • If a class is declared abstract, it cannot be instantiated. • To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it. • If you inherit an abstract class, you have to provide implementations to all the abstract methods in it. Abstraction Polymorphism • Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. • Inheritance lets us inherit attributes and methods from another class. • Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.