Week 4-5 Basic OOP Concept PDF

Summary

This document presents learning objectives, and details fundamental Object-Oriented Programming (OOP) concepts, such as classes, objects, inheritance, and polymorphism. It also covers Graphical User Interface (GUI) components in Java.

Full Transcript

Week 4 - 5 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...

Week 4 - 5 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. // File: Animal.java public class Animal { // Declare three attributes of the Animal class public String name; // Attribute to store the name of the animal public String species; // Attribute to store the species of the animal public double weight; // Attribute to store the weight of the animal // Method to simulate the animal eating public void eat() { // Print a message to the console indicating that the animal is eating System.out.println(this.name + " is eating."); } // Method to simulate the animal sleeping public void sleep() { // Print a message to the console indicating that the animal is sleeping System.out.println(this.name + " is sleeping."); } // Method to simulate the animal making a sound public void makeSound() { // Print a message to the console indicating that the animal makes a sound System.out.println(this.name + " makes a sound."); } } // File: Zoo.java public class Zoo { public static void main(String[] args) { // Create an object of Animal class named 'elephant' Animal elephant = new Animal(); // Class name the variable = new Class elephant.name = "Dumbo"; elephant.species = "Elephant"; elephant.weight = 5000; // Call methods on the 'elephant' object elephant.eat(); // Elephant performs the eating action elephant.sleep(); // Elephant performs the sleeping action } } 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. QUESTION?

Use Quizgecko on...
Browser
Browser