JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Written by: Dr. JJ Shepherd
Inheritance Inheritance Reserved word protected Reserved word super
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
The Java Programming Language
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JAVA BASICS Prepared by The Smartpath Information Systems
Chapter 2: Java Fundamentals
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
OOP Basics Classes & Methods (c) IDMS/SQL News
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
BY:- TOPS Technologies
Object Oriented Programming Lecture 2: BallWorld.
OOP: Encapsulation &Abstraction
JAVA MULTIPLE CHOICE QUESTION.
Chapter 4 Assignment Statement
Inheritance and Polymorphism
Lecture 2: Data Types, Variables, Operators, and Expressions
Packages When we name a program , we name it by class name…
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
Programming Language Concepts (CIS 635)
null, true, and false are also reserved.
Java Programming Language
Introduction to Java Programming
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Packages and Interfaces
JavaScript Reserved Words
Focus of the Course Object-Oriented Software Development
Session 2: Introduction to Object Oriented Programming
Chapter 14 Abstract Classes and Interfaces
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Presentation transcript:

JAVA COURSE 1 Computer Engineering Association

Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic static void main(STring args[]) { System.out.println(“In the main”); Hello temp = new Hello(); }

Command to compile the first program  In java, we use the jdk, which can be downloaded from the website  After installation, if we don't want to give the absolute path everytime to compile the program, we need to set the path  In windows 2000, go to [control panel]->[setting]- >[system]->[advance] and change the system enviroment variable, PATH, adding the path of [jdk directory]\bin  In linux, you need to edit the.bash_profile file  Add Path=[jdkdirectory]/bin  Export Path  Exec bash --login to make it effective  For windows 95/95/me, you need to edit the autoexec.bat file and add the path as Path =... same as above

Package  Java use the package mechanism to resolve all the class name  e.g., there are two classes as Hello1, Hello1  But they are different, as they are put in diofferent directory.  How about we import them, as if include mechanism in C++, C  So we simple type import [directory].Hello1;  And thus we can use it.  The java put all their classes in [jdkdirectory]\clases

Simple program to demonstrate package  Before we need to set the classpath  Same as before setting path, but we set CLASSPATH now  CLASSPATH=\root\demo:\root\j2sdk1.4.0\cla sses  Then we type the following program as shown

Syntax: Class definition  Class definition is defined by the following words  public(optional)  final(optional)  abstract(optional)  If we declare final, then we cannot declare abstract, vice versa. And the poition of the above words is not rigid.  Class  Class name(same as file name)  Extends (optional)  implements(optional)  {}

Meaning of the class keyword  Public means all classes can access it, whetherit is in the same package or not  If not public, then the class is accessed only by the class in the same package.  Final means the class cannot be inherited  Abstract means the class must be inherited.  Extends means to inherit a class  Implements means to inherit from the interface  The {} is needed and should not be omitted.

Syntax about method  Access modifier: [public|private|protected|default]  [abstract|final|synchronized|native|static](opti onal)  The order of the above two position is not important.  Return type [type|void]  Name of method  Argument  Exception throw(optional)  ()(abstract or interface method don't need ())

Meaning of these class syntax  First key word

 Final is the method declares a method that cannot be overridden in the subclass  Abstract means the method that should be implement in the subclass  Static means the method can be access though no object is created, but they can access instance variable or static variable  Synchronized is used in mutil-threading  Native means it is in C|C++ code  Void means no return type  Throw, we explain later

Variable  They are divided into following types:  Static variable  Instance variable  Local variable  Final variable  Transient variable  Volatile variable

 CLASS SCOPE SYNTAX:  Public, private, default, protected  Transient, final, static, volatile  type  Name  LOCAL VARIABLE (DECLARE WITHIN METHOD)  Final(optional)  Type  Name

Meaning of these words  The modifier are the same as method modifier.  Final means, const if C, C++  Static means the variable can be accessed even the object is not there.  Transient, volatile, talk later

Class variable characteristics  Access through object  Like Hello temp = new Hello();  temp.you, (assumed we have you variable)  Static variable can be accessed though no object is there. Hello.temp no object is created.

Local variable  Life Only within the same  Dead after the method

Data type  In java, everything is class, except the following data type, which is not class  Int  Char  Boolean  Float  Double  Byte(8 bit)  Long (64 bit)  Short(16 bit)  String is object. We will talk about it later

About concept of referenial variable  e.g Hello temp = new Hello();  The, there is only one object allocated in heap area.  What is heap and stack?  Stack is used for the store of local variable  And heap is for the dynamic allocation of memory, when we use new keyword, we allocate memory for the object.  How about we type Hello temp2 = temp  They refer to the same object!.

Concept of object -oriented programming  Encapsulation  We package the variable within a class, and so we ensure some variable are not modified from outside  Inheritance  We need to know the concept of ISA and HASA  Like there is a employer class  And we create a hour_employeer class to inherit from it.  The main point is we ask ourselves, is hour_work a type of employee?

 Casting  As we know hour_employee is a type of employee and inherit from it and thus we can say hour_employee is employee  e.g.  Employee temp = new hour_employee  It works  But now it is employee object  The above is called static casting which cast within compile time  And (hour_employee) temp, is called dynamic casting, that a superclass type want to cast itself to be the subclass. It may not work

 Polymorphism  We write a method a() in the super class superclass  And a() in the subclass sub, which extends superclass  We declare  Superclass temp = new subclass()  temp.a() is called not based on the type, but the actual object allocated in the heap memory  How about no a() declare in the sub?  A() is called based on the type  And this fexibility allow easy programming  For example we write a method called  Public void outName(Superclass a)  We can write outName(subclass)