Uploaded by orchardwhispers

Java - Object Oriented Programming

advertisement
Java Programming
The Java Platform
 The Java Platform consists of the
 Java Development Kit (JDK)
 Java Runtime Environment (JRE)
 JVM (Java Virtual Machine)
 The JVM is a platform independent way to run Java
programs. Java programs are interpreted by the JVM.
The Java Programs are agnostic of the Operating
system they are running on however the JVM is written
per operating system
 In order to run your Java Program on your operating
system you need to install the JVM
Java Platform
 JDK
 Provides the necessary tools and application libraries to
write Java code
 Contains compilers, debuggers and everything that is
contained within the JRE
 JRE
 Implementation of the JVM
 Container where the Java programs run
Google for Answers
 Stackoverflow
 Oracle documentation
Installing Java
 Detailed installation instructions depending on your
Operating System are available on Oracle’s site at :
 Windows :
http://docs.oracle.com/javase/8/docs/technotes/guides/ins
tall/windows_jdk_install.html#A1097272
 Mac : Typically your mac should come with Java preinstalled and you should not need to worry about
installing anything
 You can test if Java is installed on your system by going
to the command prompt on Windows or the Terminal on
Mac and typing
 java –version
 This will give you the version for the Java you have installed
IDE
 Integrated Development Environment : Tool to make writing
Java Code very easy
 It comes with all the frills needed to quickly write code
 Popular IDE’s




NetBeans
Eclipse
STS
IntelliJ
 Recommended IDE’s
 Eclipse & NetBeans
 Download Eclipse from
 https://www.eclipse.org/downloads/eclipse-packages/
 If you are on campus Eclipse comes pre-installed with the
computers
Your First Java Program
public class HelloWorld {
public static void main(String args[]){
System.out.println("Hello World!!");
}
}
Analyzing the code
 All the Java files end with a .java extension
 In your code your file ended with HelloWorld.java
 The Java Compiler (javac) compiles the .java program
and creates a .class file
 The .class file is the byte code that your .java file is
converted to
 This is the file that can be now be read and interpreted by
the JVM
Further dissection of the code
 As you can see the first thing we did was
 public class HelloWorld{
 public is a Java keyword meaning something that can be
accessed by anyone
 class is a Java keyword for a class
 HelloWorld is the name of your class
 Everything should be inside {} brackets
 Now every class has methods and attributes
 The first method you see is main()
 On top of the code we see package com.test.mycode
 This is the folder structure where you code lives
public static void main(String[] args)
 main is the method name
 public means it is accessible by anyone
 args is the arguments that can be passed to this method
(we will learn about String [] later)
 static is another keyword in Java. Anything marked static
means that it can be accessed by
ClassName.methodname() without creating a new object
Object Oriented
Programming Concepts
3 Key Concepts
 Encapsulation : is hiding the information of an object
from other code trying to access it. The only way to
access the object’s attributes is through the methods in
the class. Encapsulation helps protect the internal
methods of a class by making them private.
 Inheritance : With Inheritance a child class can inherit
the properties of a parent class. This helps in not
repeating code in multiple places. The common code
can sit in the parent class.
 Polymorphism : means once class can take several
forms. We can create a single interface and have
multiple classes implement the same interface
What is a class?
 As we have seen from before, Object Oriented
Programming (OOP) is the way to write code now
 With OOP everything we create is an object … a
computer, book, bookstore, person
 Every object has certain attributes like a Person has a
name, age, social security etc.
 A Class is a blueprint to create an object
 Java has a keyword new to create a new Object
What is an Object?
 A class is a blueprint to create objects and an object is
an instance of a class
 So for instance a Person class can create several
Person Objects using the blueprint which is the class to
create those objects
 Here is how it is done : Person p = new Person
 With the new keyword you can create a new Person
Object
Example Of a Class in Java
class Person{
String name;
int age;
String ssn;
}
 As you can see a Person class has certain attributes
 When we do Person p = new Person we create a new
object p for the Person class. We can create several
person objects, p1, p2 etc. with the Person class as it is
a blue print to create Person objects
private, public, protected
 You can specify how to access your methods and
variables in Java
 A private variable can only be accessed within the {}
 A public variable can be accessed from anywhere
 A protected variable can only accessed within the same
package
Download