JAVA BASICS Prepared by The Smartpath Information Systems www.thesmartpath.in.

Slides:



Advertisements
Similar presentations
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Chapter 10 Classes Continued
Session-02. Objective In this session you will learn : What is Class Loader ? What is Byte Code Verifier? JIT & JAVA API Features of Java Java Environment.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
University of Houston-Clear Lake Proprietary© 1997 Evolution of Programming Languages Basic cycle of improvement –Experience software difficulties –Theory.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
POS 406 Java Technology And Beginning Java Code
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance in the Java programming language J. W. Rider.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming in Java CSCI-2220 Object Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
ISBN Object-Oriented Programming Chapter Chapter
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Introduction to Object-oriented Programming
Object Oriented Programming in
OOP: Encapsulation &Abstraction
JAVA MULTIPLE CHOICE QUESTION.
Before You Begin Nahla Abuel-ola /WIT.
Java Primer 1: Types, Classes and Operators
Internet and Java Foundations, Programming and Practice
Chapter 3: Using Methods, Classes, and Objects
University of Central Florida COP 3330 Object Oriented Programming
Interface.
Interfaces.
CISC/CMPE320 - Prof. McLeod
Introducing Java.
Presentation transcript:

JAVA BASICS Prepared by The Smartpath Information Systems

Index 1.Java Technology 2.Java Programming 3.Java Programming (Cont.) 4.The Java Platform 5.Java Bytecode 6.Java Virtual Machine 7.Java Runtime Environment (JRE) 8.Java Software Development Kit (Java SDK) 9.Java Object-Oriented Programming Concepts 10.What Is an Object? 11.What Is a Class?

12. What Is Inheritance? 13.What Is an Interface? 14.What Is an Polymorphism? 15. Legal Identifiers 16.Source File Declaration Rules 17.Source File Declaration Rules (Cont.) 18.Class Declarations and Modifiers 19.Interface Implementation 20.Interface Implementation(Cont.) 21.Interface Implementation(Cont.) 22.Member Access Modifiers 23.Member Access Modifiers(Cont.) 24.Member Access Modifiers(Cont.)

25.Local Variables 26. Other Modifiers Members 27. Other Modifiers Members(Cont.) 28. Methods with var-args 29.Variable Declarations 30.Variable Declarations (Cont.) 31.Array Declarations 32.Static Variables and Methods 33.References

Java Technology  Java technology is both a programming language and a platform.  Simple  Object oriented  Distributed  Multithreaded  Dynamic  Architecture neutral  Portable  High performance  Robust  Secure

Java Programming  Source code is first written in plain text files ending with the.java extension.  Source files are then compiled into.class files by the javac compiler. A.class file does not contain code that is native to your processor; it instead contains bytecodes -- the machine language of the Java Virtual Machine (Java VM).  The java launcher tool then runs your application with an instance of the Java Virtual Machine.

Java Programming (Cont.) Java VM is available on many different operating systems, the same class files are capable of running on  Microsoft Windows  the Solaris™ Operating System (Solaris OS)  Linux, or Mac OS. Etc. Through the Java VM, the same application is capable of running on multiple platforms.

The Java Platform A platform is the hardware or software environment in which a program runs. Since Java has its own runtime environment (JRE) and API, it is called platform.The Java platform has two components: The Java Virtual Machine The Java Application Programming Interface (API) The API is a large collection of ready-made software components that provide many useful capabilities. It is grouped into libraries of related classes and interfaces; these libraries are known as packages.

Java Bytecode Java programs written in the Java language are compiled into Java bytecode which can be executed by the Java Virtual Machine. The Java bytecode is stored in binary.class files.

Java Virtual Machine  Java is an interpreted language.  The Java language is compiled into Java bytecode.  This Java bytecode is then executed by the Java Virtual Machine.  The Java Virtual Machine is like a computer. It can execute Java bytecode just like a PC can execute assembler instructions.  The Java Virtual Machine is a program itself

Java Runtime Environment (JRE) The Java Runtime Environment (JRE) is the Java Virtual Machine and the standard Java APIs coming with Java Standard Edition (JSE). The JRE contains enough to execute a Java application, but not to compile it.

Java Software Development Kit (Java SDK) The Java Software Development Kit (Java SDK) is the JRE plus the Java compiler, and a set of other tools. If you need to develop Java programs you need the full Java SDK. The JRE is not enough then. Only the full Java SDK contains the Java compiler which turns your. java source files into byte code.class files.

Java Object-Oriented Programming Concepts What Is an Object? What Is a Class? What Is Inheritance? What Is an Encapsulation? What Is Polymorphism? What Is an Interface? What Is an Object?

An Object is a particular concrete instance of Class. Object is real world entity, which has property and behavior Man StudentTree Car

What is class? A class is a blueprint or prototype from which objects are created which has attributes and behavior, we call attribute as members and behavior as methods.

What Is Inheritance? Inheritance is the process by which one object acquires the properties of another object. Inheritance provides a powerful and natural mechanism for organizing and structuring your software.

What Is an Interface? An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface.

What Is an Polymorphism? Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Legal Identifiers  Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number!  After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers.  In practice, there is no limit to the number of characters an identifier can contain.  You can't use a Java keyword as an identifier.  Identifiers in Java are case-sensitive; foo and FOO are two different identifiers.

Source File Declaration Rules  There can be only one public class per source code file.  If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { } must be in a source code file named Dog.java.  If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.  If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file.

Source File Declaration Rules (Cont.)  If there are no package or import statements, the class declaration must be the first line in the source code file.  import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.  A file can have more than one nonpublic class.  Files with no public classes can have a name that does not match any of the classes in the file.

Class Declarations and Modifiers class MyClass { } This code compiles just fine, but you can also add modifiers before the class declaration. Modifiers fall into two categories: ■ Access modifiers: public, protected, private. ■ Non-access modifiers (including strictfp, final, and abstract).

Interface Implementation ❑ Interfaces are contracts for what a class can do, but they say nothing about the way in which the class must do it. ❑ Interfaces can be implemented by any class, from any inheritance tree. ❑ An interface is like a 100-percent abstract class, and is implicitly abstract whether you type the abstract modifier in the declaration or not. ❑ An interface can have only abstract methods, no concrete methods allowed. ❑ Interface methods are by default public and abstract—explicit declaration of these modifiers is optional. ❑ Interfaces can have constants, which are always implicitly public, static, and final.

Interface Implementation(Cont.) ❑ Interface constant declarations of public, static, and final are optional in any combination. ❑ A legal non abstract implementing class has the following properties: ❑ It provides concrete implementations for the interface's methods. ❑ It must follow all legal override rules for the methods it implements. ❑ It must not declare any new checked exceptions for an implementation method. ❑ It must not declare any checked exceptions that are broader than the exceptions declared in the interface method. ❑ It may declare runtime exceptions on any interface method implementation regardless of the interface declaration.

❑ It must maintain the exact signature (allowing for covariant returns) and return type of the methods it implements (but does not have to declare the exceptions of the interface). ❑ A class implementing an interface can itself be abstract. ❑ An abstract implementing class does not have to implement the interface methods (but the first concrete subclass must). ❑ A class can extend only one class (no multiple inheritance), but it can implement many interfaces. ❑ Interfaces can extend one or more other interfaces. ❑ Interfaces cannot extend a class, or implement a class or interface. Interface Implementation(Cont.)

Member Access Modifiers Methods and instance (nonlocal) variables are known as "members." ❑ Members can use all four access levels: public, protected, default, private. ❑ Member access comes in two forms: ❑ Code in one class can access a member of another class. ❑ A subclass can inherit a member of its super class. ❑ If a class cannot be accessed, its members cannot be accessed. ❑ Determine class visibility before determining member visibility. ❑ public members can be accessed by all other classes, even in other packages. ❑ If a super class member is public, the subclass inherits it—regardless of package.

Member Access Modifiers(Cont.) ❑ Members accessed without the dot operator (.) must belong to the same class. ❑ this. always refers to the currently executing object. ❑ this.aMethod() is the same as just invoking aMethod(). ❑ private members can be accessed only by code in the same class. ❑ private members are not visible to subclasses, so private members cannot be inherited. ❑ Default and protected members differ only when subclasses are involved: ❑ Default members can be accessed only by classes in the same package. ❑ protected members can be accessed by other classes in the same package, plus subclasses regardless of package.

❑ protected = package plus kids (kids meaning subclasses). ❑ For subclasses outside the package, the protected member can be accessed only through inheritance; a subclass outside the package cannot access a protected member by using a reference to a super class instance (in other words, inheritance is the only mechanism for a subclass outside the package to access a protected member of its super class). ❑ A protected member inherited by a subclass from another package is not accessible to any other class in the subclass package, except for the subclass' own subclasses. Member Access Modifiers(Cont.)

Local Variables  Local (method, automatic, or stack) variable declarations cannot have access modifiers. ❑ final is the only modifier available to local variables. ❑ Local variables don't get default values, so they must be initialized before use.

Other Modifiers Members  final methods cannot be overridden in a subclass. ❑ abstract methods are declared, with a signature, a return type, and an optional throws clause, but are not implemented. ❑ abstract methods end in a semicolon—no curly braces. ❑ Three ways to spot a non-abstract method: ❑ The method is not marked abstract. ❑ The method has curly braces. ❑ The method has code between the curly braces. ❑ The first non abstract (concrete) class to extend an abstract class must implement all of the abstract class' abstract methods.

❑ The synchronized modifier applies only to methods and code blocks. ❑ synchronized methods can have any access control and can also be marked final. ❑ abstract methods must be implemented by a subclass, so they must be inheritable. For that reason: ❑ abstract methods cannot be private. ❑ abstract methods cannot be final. ❑ The native modifier applies only to methods. ❑ The strictfp modifier applies only to classes and methods. Other Modifiers Members (Cont.)

Methods with var-args ❑ As of Java 5, methods can declare a parameter that accepts from zero to many arguments, a so-called var-arg method. ❑ A var-arg parameter is declared with the syntax type... name; for instance: doStuff(int... x) { } ❑ A var-arg method can have only one var-arg parameter. ❑ In methods with normal parameters and a var-arg, the var-arg must come last.

Variable Declarations Instance variables can ❑ Have any access control ❑ Be marked final or transient ❑ Instance variables can't be abstract, synchronized, native, or strictfp. ❑ It is legal to declare a local variable with the same name as an instance variable; this is called "shadowing." ❑ final variables have the following properties: ❑ final variables cannot be reinitialized once assigned a value. ❑ final reference variables cannot refer to a different object once the object has been assigned to the final variable.

Variable Declarations(Cont.) ❑ final reference variables must be initialized before the constructor completes. ❑ There is no such thing as a final object. An object reference marked final does not mean the object itself is immutable. ❑ The transient modifier applies only to instance variables. ❑ The volatile modifier applies only to instance variables.

Array Declarations  Arrays can hold primitives or objects, but the array itself is always an object. ❑ When you declare an array, the brackets can be to the left or right of the variable name. ❑ It is never legal to include the size of an array in the declaration. ❑ An array of objects can hold any object that passes the IS-A (or instance of) test for the declared type of the array. For example, if Horse extends Animal, then a Horse object can go into an Animal array.

Static Variables and Methods  They are not tied to any particular instance of a class. ❑ No classes instances are needed in order to use static members of the class. ❑ There is only one copy of a static variable / class and all instances share it. ❑ Static methods do not have direct access to non-static members.

References   Book: SCJP By-Kathy Sierra,Bert Bates

THANK YOU!!!