Lesson-1-OOP-Intro.pdf
Document Details
Uploaded by FancierPentagon3062
Tags
Full Transcript
INTRODUCTION TO OBJECT- ORIENTED PROGRAMMING (OOP) Procedure-Oriented Programming ▪ Procedure-oriented programming (POP), also known as structured programming, is a programming paradigm that focuses on organizing a program into a set of functions or procedures. ▪ In this paradigm, the progr...
INTRODUCTION TO OBJECT- ORIENTED PROGRAMMING (OOP) Procedure-Oriented Programming ▪ Procedure-oriented programming (POP), also known as structured programming, is a programming paradigm that focuses on organizing a program into a set of functions or procedures. ▪ In this paradigm, the program is designed as a sequence of procedures, each of which performs a specific task. Console Programming ▪ Is a java programming method that uses ‘text-only’ interfaces Swing Programming ▪ Allows the developers to create graphical user interface Object Oriented Programming ▪ Object-oriented programming (OOP) is a programming paradigm or methodology that uses objects as the fundamental building blocks of software development. ▪ Objects can represent real-world entities with attributes (data) and behavior (functions or methods). Key Differences Organization Procedural: Data and functions are separate. Functions operate on global or passed data. OOP: Data (attributes) and functions (Methods) are encapsulated within objects. Key Differences Reusability Procedural :Functions can be reused, but managing related data requires more manual effort. OOP: Objects can be reused and extended. You can create new classes based on existing ones (inheritance). Key Differences Maintainability Procedural: As the program grows, managing and understanding the flow of data and functions can become complex. OOP: Encapsulation and modularity make the code easier to maintain, as changes to a class are localized. KEY CONCEPTS OF OOP ▪ ENCAPSULATION – Bundling attributes and methods that operate on the data within one unit. ▪ INHERITANCE - The mechanism by which one class can inherit the properties and behaviors of another class. ▪ POLYMORPHISM - Ability of different objects to respond to the same function or method call in different ways. ▪ ABSTRACTION – Hiding complex implementation details and showing only the necessary features of an object. ▪ MODULARITY – allows to code and understand just a particular part of the system without the need to understand the whole system ▪ REUSABILITY – gives the flexibility to use certain programs repeatedly ▪ CONSTRUCTOR - A constructor is a special method in a class used to initialize the object's attributes when an object is created. Classes ▪ A class is a blueprint or template for creating objects. ▪ A foundation from which individual objects are created. ▪ It defines the structure (attributes or properties) and behavior (methods or functions) that its objects will have. ▪ Objects of the same class share the same structure and behavior. ▪ Super Class – base class, ancestor class, parent class ▪ Sub Class – derived class, descendant class, child class ▪ Non-static Class – class that should be instantiated ▪ Static Class – doesn’t require instantiation Object ▪ an instance of class. It has attributes and behaviors. ▪ Each object can have unique values for its attributes, even if they are created from the same class. Attributes ▪ Attributes (also known as fields or properties) are variables within a class that represent the state or characteristics of objects created from that class. They store data that can be manipulated by methods. State ▪ The current values of an object's attributes at any given time.The state can change over time as the object interacts with methods or other objects. Example: ▪ Class – Car ▪ Methods – start(), stop(), accelerate() ▪ Object – myCar ▪ Attributes – color = “red”, engineState = “off” ▪ State of myCar changes when the start() method is invoked, changing engineState to “on” Method ▪ an action done by an object ▪ Methods are functions defined within a class that specify the behavior of objects of that class. They perform actions and manipulate data within the objects. Abstract Method – is an empty method Interfaces ▪ A collection of methods that indicate that a class has some behavior in addition to what it inherits from its superclasses. ▪ Interfaces are used to define a common set of methods that various classes can implement according to their specific needs. Types of Method Instance Method – Operate on an instance of the class (an object). Static Method - Belong to the class rather than any object instance; they don't modify object attributes and can be called on the class itself. Packages ▪ A way of grouping related classes and interfaces in a structured way. ▪ Enabled groups of classes to be available only if they are needed. ▪ Eliminate potential conflicts among class names in different groups of classes. Few things to know… ▪ The class libraries in Java are contained in a package called java. ▪ By default, your Java classes have access only to the classes in java.lang (basic language features). To use classes from any other package, you have to refer to them explicitly by package name or import them on your source file Few things to know… ▪ To refer to a class within a package, you must normally use the full package name. For example, because the Color class is contained in the java.awt package, you refer to it in your programs with notation java.awt.Color Programming Basics Classes, Interface, Packages import java.io.BufferedReader java.io java.lang java.util import java.util.Scanner javax.swing import javax.swing.Frame Way of grouping related classes Instantiation ▪ a process of creating object of the class Ex. Instantiate an object named ‘objectAko’ from class ‘MyClass’ (assuming that MyClass requires no arguments or parameters) MyClass objectAko = new MyClass ( ); Scanner Class ▪ Scanner class is used to read the user’s input from the java.util package. ▪ Declare an input Scanner in the main method to be able to use the methods included to the Scanner class. ▪ Syntax: Scanner scan = new Scanner (System.in); Scanner Class Methods Description ▪ nextLine() ▪ String ▪ nextInt() ▪ Scans the next token of the input as an int. ▪ nextDouble() ▪ Scans the next token of the input as a double. ▪ nextFloat() ▪ Scans the next token of the input as a float. Scanner Class Methods Description ▪ nextBoolean() ▪ Scans the next token of the input into a boolean value and returns that value ▪ nextByte() ▪ Scan the input as a byte ▪ Scans the next token of ▪ nextLong() the input as a long int. ▪ Scans the next token of ▪ nextShort() the input as a short int. BufferedReader Class ▪ Also used to read the user’s input from the java.io package. ▪ Syntax: BufferedReader read = new BufferedReader (new InputStreamReader(System.in)); BufferedReader Class Methods Description ▪ read() ▪ Reads a single character ▪ readLine() ▪ Reads a line of text. Comments in Java ▪ Single Line – use double slash // ? Ex. // defines a class named HelloWorld ▪ Multi-Line – use ? Ex. public class SampleNo1 // defines a class named SampleNo1 { public static void main (String [ ] args) { System.out.println (“ Java is fun! “); System.out.print (“ Let’s enjoy learning it!”); } // end of main } // end of class public static void main (String [ ] args) ▪ This is called the 'main method' and is the entry point to your program for the JVM. public means any other method in the code can access it (as opposed to a private method) static means the Java VM will automatically execute the method void means that it will not return anything (since functions / method are usually expected to return a value.) main is the name of the method. (String args) means that the arguments of the method named main should be strings. In the Java programming language, every application must contain a main method whose signature is: public static void main(String[ ] args) ▪ The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. ▪ The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program. ▪ The main method accepts a single argument: an array of elements of type String. We can use both String[ ] args or String args[]. The Java compiler would accept both forms. You can name the argument anything you want, but most programmers choose "args" or "argv". System.out.println (“ Java is fun ”); System is a class made available by Java to let you manipulate various operating system related objects. It's part of java.lang namespace. out, being one of those objects, is a static publicly available object inside that class. The object itself, representing standard output, is an instance of java.io.PrintStream class. print ( ) is a method of java.io.PrintStream class that lets you output some text into the stream println( ) method, gets you to the new line after the text. System.out.println (“ Java is fun ”); System.out is an object used for sending output to the screen; print is the method that this object carries out in order to send what it is in the parenthesis to the screen. The item or items inside the parenthesis are called arguments and provide the information the method needs to carry out its action. You don't need to import java.io.* because System is part of available to all Java calls package java.lang. Even though out is an instance of java.io.PrintStream, it is still declared *inside* the System class and therefore, you do not need to import java.io.