prelim

Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Object Oriented Programming: 

Object Oriented Programming Joining the “I Understand Classes and Objects Society”

Object-oriented programming (OOP): 

Object-oriented programming (OOP) Object-oriented design is a technique that focuses design on objects and classes based on real world scenarios . Programming techniques may include features such as encapsulation , polymorphism , and inheritance .

Encapsulation: 

Encapsulation

Polymorphism: 

Polymorphism

Inheritance: 

Inheritance

Fundamental Concepts: 

Fundamental Concepts Joining the “I Understand Classes and Objects Society”

OOP FUNDAMENTALS: 

OOP FUNDAMENTALS CLASS OBJECT ATTRIBUTE METHOD CONSTRUCTOR PACKAGE ENCAPSULATION ABSTRACTION INHERITANCE POLYMORPHISM INTERFACE

Class: 

Class allows you to define new data types. It serves as a blueprint , which is a model for the objects you create based on this new data type. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors).

Another example:: 

Another example: Gordon College Student // Class Carlo // object in class student Anna // another object in class student Instructor // Class Chris // object in class Instructor Alvin // another object in class Instructor Armilyn // another object in class Instructor

Program: 

Program class Student { void enroll() { System.out.println(“Student class”);} } class Anna extends Student { void enroll(){ System.out.println(“Anna is a student of Gordon Collge”); } } class Carlo extends Student { void enroll() { System.out.println(“Carlo is a student of Gordon Collge”); } } public class GC { public static void main (String[]args){ Anna stud1 = new Anna(); Carlo stud2 = new Carlo(); Stud1.enroll(); Stud2.enroll(); }}

PowerPoint Presentation: 

A blueprint which is a model for the objects you create based on the new datatype. The code for a class should be relatively self-contained (generally using encapsulation ). Collectively, the properties and methods defined by a class are called members.

OOP FUNDAMENTALS: 

OOP FUNDAMENTALS CLASS OBJECT ATTRIBUTE METHOD CONSTRUCTOR PACKAGE ENCAPSULATION ABSTRACTION INHERITANCE POLYMORPHISM INTERFACE

Object: 

Object An object is an identity that has a state, behavior and identity. A pattern of a class . Created every time you instantiate a class using a new keyword.

PowerPoint Presentation: 

Since programming languages use variables to access objects, the terms object and variable are often used interchangeably. However, until memory is allocated, an object does not exist.

PowerPoint Presentation: 

The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have ; the object Lassie is one particular dog, with particular versions of the characteristics . A Dog has fur; Lassie has brown-and-white fur .

Example of object: 

Example of object class Student{ void enroll(){ System.out.println(“Student class”);} } class Anna extends Student{ void enroll(){ System.out.println(“Anna is a student of Gordon Collge”); } } class Carlo extends Student{ void enroll(){ System.out.println(“Carlo is a student of Gordon Collge”); } } public class GC{ public static void main(String[]args){ Anna stud1 = new Anna (); // stud1 is an object Carlo stud2 = new Carlo (); // stud2 is an object Stud1 .enroll(); // execution of an object Stud2 .enroll(); // execution of an object }}

OOP FUNDAMENTALS: 

OOP FUNDAMENTALS CLASS OBJECT ATTRIBUTE METHOD CONSTRUCTOR PACKAGE ENCAPSULATION ABSTRACTION INHERITANCE POLYMORPHISM INTERFACE

Attribute: 

Attribute Attribute refers to the data element of an object. It stores information about the object

Examples class Car: 

Examples class Car int wheel = 4; int engine = 1; int doors = 4;

Example Student: 

Example Student Class Anna Long hair // attributes Blouse //attributes Class Carlo Short hair //attributes Polo/pants //attributes

OOP FUNDAMENTALS: 

OOP FUNDAMENTALS CLASS OBJECT ATTRIBUTE METHOD CONSTRUCTOR PACKAGE ENCAPSULATION ABSTRACTION INHERITANCE POLYMORPHISM INTERFACE

Method: 

Method A method describes the behavior of an object. It is also called a function or a procedure.

PowerPoint Presentation: 

class Student{ void enroll() { System.out.println(“Student class”);} } // enroll is a method class Anna extends Student{ void enroll(){ System.out.println(“Anna is a student of Gordon Collge”); } // enroll is a method } class Carlo extends Student{ void enroll() { // enroll is a method System.out.println(“Carlo is a student of Gordon Collge”); } } public class GC{ public static void main(String[]args){ Anna stud1 = new Anna(); // stud1 is an object Carlo stud2 = new Carlo(); // stud1 is an object Stud1.enroll(); // execution of an object Stud2.enroll(); // execution of an object }}

OOP FUNDAMENTALS: 

OOP FUNDAMENTALS CLASS OBJECT ATTRIBUTE METHOD CONSTRUCTOR PACKAGE ENCAPSULATION ABSTRACTION INHERITANCE POLYMORPHISM INTERFACE

Constructor: 

Constructor A constructor is a special type of method used for creating and initializing a new object.

Example of Constructor: 

Example of Constructor // a class with 3 constructors, differing by their parameters class t2 { //class name t2 () { // constructor System.out.println("empty arg called!"); } t2 (int n) { //another constructor System.out.println("int one called!"); } t2 (double n) { //another constructor System.out.println("double one called!"); } } class t1 { public static void main(String[] arg) { // creating 3 objects of t2. // when each object are created, Java automatically calls the right constructor t2 x3 = new t2(); //execution of constructor t2 x4 = new t2(3); //execution of constructor t2 x5 = new t2(3.0); //execution of constructor } }

PowerPoint Presentation: 

class Aconstructor{ int x,y; ______________ (int a, int b ){ x = a ; y = b ; } Aconstructor (){ } } public class B{ public static void main(String[] args) { Aconstructor cons = new ________________ (); cons . x = 2; cons . y = 3; _______________(Value of x in another class : " + cons . x ); System.out.println("Value of y in another class : " + cons . y ); } }

PowerPoint Presentation: 

class Aconstructor{ int x,y; Aconstructor (int a, int b ){ // a and b can have a value later x = a ; //the value of a will be stored in x y = b ; //the value of b will be stored in y } Aconstructor (){ } } public class B{ public static void main(String[] args) { Aconstructor cons = new Aconstructor (); cons . x = 2; cons . y = 3; System.out.println("Value of x in another class : " + cons . x ); System.out.println("Value of y in another class : " + cons . y ); } }

OOP FUNDAMENTALS: 

OOP FUNDAMENTALS CLASS OBJECT ATTRIBUTE METHOD CONSTRUCTOR PACKAGE ENCAPSULATION ABSTRACTION INHERITANCE POLYMORPHISM INTERFACE

Package: 

Package Refers to a grouping of classes or subpackages a concept programming where related classes and interfaces are grouped together

PowerPoint Presentation: 

Java packages can be stored in compressed files called JAR files , allowing classes to download faster as a group rather than one at a time. To use a package inside a Java source file, it is convenient to import the classes from the package with an import statement. The statement import javax.swing.*;

Core packages in Java SE 6: 

Core packages in Java SE 6 java.lang — basic language functionality and fundamental types java.util — collection data structure classes java.io — file operations java.math — multiprecision arithmetics java.nio — the New I/O framework for Java java.net — networking operations, sockets, DNS lookups, ... java.security — key generation, encryption and decryption java.sql — Java Database Connectivity (JDBC) to access databases java.awt — basic hierarchy of packages for native GUI components javax.swing — hierarchy of packages for platform-independent rich GUI components

OOP FUNDAMENTALS: 

OOP FUNDAMENTALS CLASS OBJECT ATTRIBUTE METHOD CONSTRUCTOR PACKAGE ENCAPSULATION ABSTRACTION INHERITANCE POLYMORPHISM INTERFACE

Encapsulation: 

Encapsulation Refers to the principle of hiding design implementation information that are not relevant to the current object the principle of information hiding is the hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed.

Example: 

Example class Encapsulation { private int secret; }

PowerPoint Presentation: 

/* File name : EncapTest.java */ public class EncapTest{ private String name ; private String idNum ; private int age ; public int getAge() { return age ; } public String getName() { return name ; } public String getIdNum() { return idNum ; } public void setAge ( int newAge){ age = newAge; } public void setName (String newName){ name = newName; } public void setIdNum ( String newId){ idNum = newId; } }

PowerPoint Presentation: 

/* File name : RunEncap.java */ public class RunEncap{ public static void main(String args[]){ EncapTest encap = new EncapTest(); encap. setName ("James"); encap. setAge (20); encap. setIdNum ("12343ms"); System.out.print("Name : " + encap.getName() + " Age : "+ encap.getAge() ); } }

Output: 

Output Name : James Age : 20 //the word set means inserting information inside // the word get means getting or showing the output or information