INTRODUCTION TO OOP AND JAVA FUNDAMENTALS N.ANSGAR MARY AP/IT
Unit I • Object Oriented Programming - Abstraction – objects and classes - Encapsulation- Inheritance- Polymorphism- OOP in Java – Characteristics of Java – The Java Environment - Java Source File - Structure Compilation. Fundamental Programming Structures in Java – Defining classes in Java – constructors, methods - access specifiers - static members - Comments, Data Types, Variables, Operators, Control Flow, Arrays, Packages - JavaDoc comments.
OBJECT-ORIENTED PROGRAMMING Object-Oriented Programming (OOP) is a programming language model organized around objects rather than actions and data. An object-oriented program can be characterized as data controlling access to code. Concepts of OOPS • Object • Class • Inheritance • Polymorphism • Abstraction • Encapsulation
OBJECT • Object means a real word entity such as pen, chair, table etc. • Any entity that has state and behavior is known as an object. • Object can be defined as an instance of a class. • An object contains an address and takes up some space in memory. Objects can communicate without knowing details of each other's data or code, the only necessary thing is that the type of message accepted and type of response returned by the objects. • An object has three characteristics: • state: represents data (value) of an object. • behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc. • identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.
Example of an object : dog1
CLASS • Collection of objects is called class. • It is a logical entity. • A class can also be defined as a blueprint from which you can create an individual object. • A class consists of Data members and methods. • The primary purpose of a class is to hold data/information. • The member functions determine the behavior of the class, i.e. provide a definition for supporting various operations on data held in the form of an object. • Class doesn’t store any space.
Example public class Dog { String breed; int age; String color; void barking() { } void hungry() { } void sleeping() { } }
Constructors • Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor. Following is an example of a constructor − Example public class Puppy { public Puppy() { } public Puppy(String name) { // This constructor has one parameter, name. } }
INHERITANCE • Inheritance can be defined as the procedure or mechanism of acquiring all the properties and behavior of one class to another, i.e., acquiring the properties and behavior of child class from the parent class. • When one object acquires all the properties and behaviours of another object, it is known as inheritance. • It provides code reusability and establishes relationships between different classes. A class which inherits the properties is known as Child Class(sub-class or derived class) whereas a class whose properties are inherited is known as Parent class(super-class or base class). • Types of inheritance in java: single, multilevel and hierarchical inheritance. Multiple and hybrid inheritance is supported through interface only.
POLYMORPHISM • When one task is performed by different ways i.e. known as polymorphism. For example: to draw something e.g. shape or rectangle etc.
Polymorphism is classified into two ways: Method Overloading(Compile time Polymorphism) • Method Overloading is a feature that allows a class to have two or more methods having the same name but the arguments passed to the methods are different. Compile time polymorphism refers to a process in which a call to an overloaded method is resolved at compile time rather than at run time. Method Overriding(Run time Polymorphism) • If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java. In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.
ABSTRACTION • Abstraction refers to the act of representing essential features without including the background details or explanations. For example: phone call, we don't know the internal processing. In java, we use abstract class and interface to achieve abstraction.
ENCAPSULATION • The wrapping up of data and methods into a single unit (called class) is known as encapsulation. • Data encapsulation is the most striking feature of a class. The data is not accessible to' the outside world and only those methods, which are wrapped in the class, can access it. These methods provide the interface between the object's data and the program. • i.e. mixed of several medicines. A java class is the example of encapsulation. • This insulation of the data from direct access by 'the program is called data hiding.
Message Communication: • 1. Creating classes that define objects and their behaviour. • 2. Creating objects from class definitions. • 3. Establishing communication among objects.
Dynamic Binding • Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at runtime.
DIFFERENCE BETWEEN PROCEDURE-ORIENTED AND OBJECT-ORIENTED PROGRAMMING
JAVA • Java is a robust, general-purpose, high-level programming language and a powerful software platform. It is also object-oriented, distributed, portable and multi-threaded. Java follows the 'Write - once - run - anywhere' approach. • All Java programs must run on the Java platform that has two components, i. Java Virtual Machine (JVM) Ii.Java Application Programming Interface (API). • With Java API, many types of Java programs can be developed. These include Java stand-alone applications Java Applets Java Servlets
Characteristics of Java language • Compiled and Interpreted • Platform-Independent and Portable • Object-Oriented • Robust and Secure • Distributed • Familiar, Simple. and Small • Multithreaded and Interactive • High Performance • Dynamic and Extensible
THE JAVA PROGRAMMING ENVIRONMENT:
STRUCTURE OF JAVA PROGRAM A Java program involves the following sections: • Documentation Section • Package Statement • Import Statements • Interface Statement • Class Definition • Main Method Class • Main Method Definition
COMPILATION AND EXECUTION OF A JAVA PROGRAMCompilation • First, the source .java‘ file is passed through the compiler, which then encodes the source code into a machine independent encoding, known as Bytecode. The content of each class contained in the source file is stored in a separate .class‘ file. Execution • The class files generated by the compiler are independent of the machine or the OS, which allows them to be run on any system. To run, the main class file (the class that contains the method main) is passed to the JVM, and then goes through three main stages before the final machine code is executed. These stages are: a. Class Loader b.Bytecode Verifier c.Just-In-Time Compiler
FUNDAMENTAL PROGRAMMING STRUCTURES IN JAVA • COMMENTS – System.out.println("Hello, World!"); // comment – /* and */ – /** to start and a */ to end public class JavaApplication1 { public static void main(String[] args) { System.out.println("welcome"); } }
DATA TYPES 1) Integers • Java defines four integer types: byte, short, int, and long.
2. Floating-Point Types 3. Characters It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more. For this purpose, it requires 16 bits. Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536. Although char is designed to hold Unicode characters, it can also be thought of as an integer type on which you can perform arithmetic operations 4. Booleans
Literals • 1. Integer Literals • 2. Floating-Point Literals • 3. Boolean Literals • 4. Character Literals • 5. String Literals
VARIABLES• The variable is the basic unit of storage in a Java program. • Declaring a Variable type identifier [ = value][, identifier [= value] ...] ; • int a, b, c; // declares three ints, a, b, and c. • int d = 3, e, f = 5; // declares three more ints, initializing // d and f. Dynamic Initialization: double c = Math.sqrt(a * a + b * b); The Scope and Lifetime of Variables There are two general categories of scopes: • global and • local Type Conversion and Casting -int value to a long variable Casting Incompatible Types int a; byte b; // ... b = (byte) a;
OPERATORS • 1. Arithmetic Operators:
• 2. The Bitwise Operators
• 3. Relational Operators
• 4. Boolean Logical Operators • 5. Short-Circuit Logical Operators
• 6. The Assignment Operator: • 7. The ? Operator – General form: – expression1 ? expression2 : expression3
CONTROL STATEMENTS Java‘s program control statements can be put into the following categories: • Selection statements • Iteration statements • Jump statements
1. Selection statements Java supports two selection statements: • if • switch
2. Iteration Statements Java‘s iteration statements are • for for(initialization; condition; iteration) { // body } • do-while
• while while(condition) { // body of loop }
• do-while

Introduction to oop and java fundamentals

  • 1.
    INTRODUCTION TO OOPAND JAVA FUNDAMENTALS N.ANSGAR MARY AP/IT
  • 3.
    Unit I • ObjectOriented Programming - Abstraction – objects and classes - Encapsulation- Inheritance- Polymorphism- OOP in Java – Characteristics of Java – The Java Environment - Java Source File - Structure Compilation. Fundamental Programming Structures in Java – Defining classes in Java – constructors, methods - access specifiers - static members - Comments, Data Types, Variables, Operators, Control Flow, Arrays, Packages - JavaDoc comments.
  • 4.
    OBJECT-ORIENTED PROGRAMMING Object-Oriented Programming(OOP) is a programming language model organized around objects rather than actions and data. An object-oriented program can be characterized as data controlling access to code. Concepts of OOPS • Object • Class • Inheritance • Polymorphism • Abstraction • Encapsulation
  • 6.
    OBJECT • Object meansa real word entity such as pen, chair, table etc. • Any entity that has state and behavior is known as an object. • Object can be defined as an instance of a class. • An object contains an address and takes up some space in memory. Objects can communicate without knowing details of each other's data or code, the only necessary thing is that the type of message accepted and type of response returned by the objects. • An object has three characteristics: • state: represents data (value) of an object. • behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc. • identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.
  • 7.
    Example of anobject : dog1
  • 8.
    CLASS • Collection ofobjects is called class. • It is a logical entity. • A class can also be defined as a blueprint from which you can create an individual object. • A class consists of Data members and methods. • The primary purpose of a class is to hold data/information. • The member functions determine the behavior of the class, i.e. provide a definition for supporting various operations on data held in the form of an object. • Class doesn’t store any space.
  • 11.
    Example public class Dog { Stringbreed; int age; String color; void barking() { } void hungry() { } void sleeping() { } }
  • 12.
    Constructors • Each timea new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor. Following is an example of a constructor − Example public class Puppy { public Puppy() { } public Puppy(String name) { // This constructor has one parameter, name. } }
  • 14.
    INHERITANCE • Inheritance canbe defined as the procedure or mechanism of acquiring all the properties and behavior of one class to another, i.e., acquiring the properties and behavior of child class from the parent class. • When one object acquires all the properties and behaviours of another object, it is known as inheritance. • It provides code reusability and establishes relationships between different classes. A class which inherits the properties is known as Child Class(sub-class or derived class) whereas a class whose properties are inherited is known as Parent class(super-class or base class). • Types of inheritance in java: single, multilevel and hierarchical inheritance. Multiple and hybrid inheritance is supported through interface only.
  • 16.
    POLYMORPHISM • When onetask is performed by different ways i.e. known as polymorphism. For example: to draw something e.g. shape or rectangle etc.
  • 17.
    Polymorphism is classifiedinto two ways: Method Overloading(Compile time Polymorphism) • Method Overloading is a feature that allows a class to have two or more methods having the same name but the arguments passed to the methods are different. Compile time polymorphism refers to a process in which a call to an overloaded method is resolved at compile time rather than at run time. Method Overriding(Run time Polymorphism) • If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java. In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.
  • 18.
    ABSTRACTION • Abstraction refersto the act of representing essential features without including the background details or explanations. For example: phone call, we don't know the internal processing. In java, we use abstract class and interface to achieve abstraction.
  • 19.
    ENCAPSULATION • The wrappingup of data and methods into a single unit (called class) is known as encapsulation. • Data encapsulation is the most striking feature of a class. The data is not accessible to' the outside world and only those methods, which are wrapped in the class, can access it. These methods provide the interface between the object's data and the program. • i.e. mixed of several medicines. A java class is the example of encapsulation. • This insulation of the data from direct access by 'the program is called data hiding.
  • 20.
    Message Communication: • 1.Creating classes that define objects and their behaviour. • 2. Creating objects from class definitions. • 3. Establishing communication among objects.
  • 22.
    Dynamic Binding • Dynamicbinding means that the code associated with a given procedure call is not known until the time of the call at runtime.
  • 23.
    DIFFERENCE BETWEEN PROCEDURE-ORIENTEDAND OBJECT-ORIENTED PROGRAMMING
  • 24.
    JAVA • Java isa robust, general-purpose, high-level programming language and a powerful software platform. It is also object-oriented, distributed, portable and multi-threaded. Java follows the 'Write - once - run - anywhere' approach. • All Java programs must run on the Java platform that has two components, i. Java Virtual Machine (JVM) Ii.Java Application Programming Interface (API). • With Java API, many types of Java programs can be developed. These include Java stand-alone applications Java Applets Java Servlets
  • 26.
    Characteristics of Javalanguage • Compiled and Interpreted • Platform-Independent and Portable • Object-Oriented • Robust and Secure • Distributed • Familiar, Simple. and Small • Multithreaded and Interactive • High Performance • Dynamic and Extensible
  • 27.
  • 28.
    STRUCTURE OF JAVAPROGRAM A Java program involves the following sections: • Documentation Section • Package Statement • Import Statements • Interface Statement • Class Definition • Main Method Class • Main Method Definition
  • 29.
    COMPILATION AND EXECUTIONOF A JAVA PROGRAMCompilation • First, the source .java‘ file is passed through the compiler, which then encodes the source code into a machine independent encoding, known as Bytecode. The content of each class contained in the source file is stored in a separate .class‘ file. Execution • The class files generated by the compiler are independent of the machine or the OS, which allows them to be run on any system. To run, the main class file (the class that contains the method main) is passed to the JVM, and then goes through three main stages before the final machine code is executed. These stages are: a. Class Loader b.Bytecode Verifier c.Just-In-Time Compiler
  • 31.
    FUNDAMENTAL PROGRAMMING STRUCTURES INJAVA • COMMENTS – System.out.println("Hello, World!"); // comment – /* and */ – /** to start and a */ to end public class JavaApplication1 { public static void main(String[] args) { System.out.println("welcome"); } }
  • 32.
    DATA TYPES 1) Integers •Java defines four integer types: byte, short, int, and long.
  • 34.
    2. Floating-Point Types 3.Characters It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more. For this purpose, it requires 16 bits. Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536. Although char is designed to hold Unicode characters, it can also be thought of as an integer type on which you can perform arithmetic operations 4. Booleans
  • 35.
    Literals • 1. IntegerLiterals • 2. Floating-Point Literals • 3. Boolean Literals • 4. Character Literals • 5. String Literals
  • 36.
    VARIABLES• The variableis the basic unit of storage in a Java program. • Declaring a Variable type identifier [ = value][, identifier [= value] ...] ; • int a, b, c; // declares three ints, a, b, and c. • int d = 3, e, f = 5; // declares three more ints, initializing // d and f. Dynamic Initialization: double c = Math.sqrt(a * a + b * b); The Scope and Lifetime of Variables There are two general categories of scopes: • global and • local Type Conversion and Casting -int value to a long variable Casting Incompatible Types int a; byte b; // ... b = (byte) a;
  • 37.
  • 38.
    • 2. TheBitwise Operators
  • 39.
  • 40.
    • 4. BooleanLogical Operators • 5. Short-Circuit Logical Operators
  • 41.
    • 6. TheAssignment Operator: • 7. The ? Operator – General form: – expression1 ? expression2 : expression3
  • 42.
    CONTROL STATEMENTS Java‘s programcontrol statements can be put into the following categories: • Selection statements • Iteration statements • Jump statements
  • 43.
    1. Selection statements Javasupports two selection statements: • if • switch
  • 44.
    2. Iteration Statements Java‘siteration statements are • for for(initialization; condition; iteration) { // body } • do-while
  • 45.
  • 46.