SlideShare a Scribd company logo
Java Language and OOP
Part IV
By Hari Christian
Agenda
• 01 Enum
• 02 Final
• 03 Static
• 04 Variable Args
• 05 Encapsulation
• 06 Inheritance
• 07 Polymorphism
• 08 Interfaces
• 09 Abstract Class
• 10 Nested class
• 11 JAR
Enum
• Old approach
class Bread {
static final int wholewheat = 0;
static final int ninegrain = 1;
static final int rye = 2;
static final int french = 3;
}
int todaysLoaf = Bread.rye;
Enum
• New approach
public enum Bread {
wholewheat,
ninegrain,
rye,
french
}
Bread todaysLoaf = Bread.rye;
Enum
• Enum with Constructor
public enum Egg {
small(10),
medium(20),
large(30);
Egg(int size) { this.size = size; }
private int size;
// Setter Getter
}
Enum
• Enum with Constructor
public enum JobCategory {
staff(“Staff”),
nonstaff(“Non Staff”);
JobCategory(String name) { this.name = name; }
private String name;
// Setter Getter
}
Final
• When a reference variable is declared final, it
means that you cannot change that variable to
point at some other object.
• You can, however, access the variable and
change its fields through that final reference
variable.
• The reference is final, not the referenced object.
Final
• Example:
void someMethod(final MyClass c, final int a[]) {
c.field = 7; // allowed
a[0] = 7; // allowed
c = new MyClass(); // NOT allowed
a = new int[13]; // NOT allowed
}
Static
• Field which is only one copy
• Also called class variable
• What you can make static:
1. Fields
2. Methods
3. Blocks
4. Class
Static - Fields
• Example:
class Employee {
int id; // per-object field
int salary; // per-object field
static int total; // per-class field (one only)
}
Static - Fields
• Example:
class EmployeeTest {
public static void main(String[] args) {
Employee h = new Employee();
h.id = 1;
Employee r = new Employee();
r.id = 2;
Employee.total = 10;
h.total; // 10
r.total; // 10
}
}
Static - Methods
• Also called class method
• Example:
public static int parseInt(String s) {
// statements go here.
}
int i = Integer.parseInt("2048");
Variable Arity – Var Args
• Var Args is optional
• Var Args is an Array
Var Args
public class VarArgs {
public static void main(String[] args) {
sum();
sum(1, 2, 3, 4, 5);
}
public static void sum(int … numbers) {
int total = 0;
for (int i = 0; i < numbers.length; i++) {
total += numbers[i];
}
return total;
}
Encapsulation
• Imagine if you made your class with public instance
variables, and those other programmers were setting the
instance variables directly
public class BadOO {
public int size;
}
public class ExploitBadOO {
public static void main (String [] args) {
BadOO b = new BadOO();
b.size = -5; // Legal but bad!!
}
Encapsulation
• OO good design is hide the implementation
detail by using Encapsulation
• How do you do that?
– Keep instance variables protected (with an access
modifier, often private)
– Make public accessor methods, and force calling
code to use those methods rather than directly
accessing the instance variable
– For the methods, use the JavaBeans naming
convention of set<someProperty> and
get<someProperty>
Encapsulation
• Rewrite the class
public class GoodOO {
private int size;
public void setSize(int size) {
if (size < 0) this.size = 0; // does not accept negative
else this.size = size;
}
public void getSize() {
return size;
}
}
public class ExploitGoodOO {
public static void main (String [] args) {
GoodOO g = new GoodOO();
g.setSize(-5); // it’s safe now, no need to worry
}
Encapsulation
Inheritance
• A class that is derived from another class is
called a subclass (also a derived
class, extended class, or child class)
• The class from which the subclass is derived is
called a superclass (also a base class or
a parent class)
Inheritance
• Every class has one and only one direct
superclass (single inheritance), Object
• Classes can be derived from classes that are
derived from classes that are derived from
classes, and so on, and ultimately derived from
the topmost class, Object
Inheritance
• The idea of inheritance is simple but powerful:
When you want to create a new class and there
is already a class that includes some of the code
that you want, you can derive your new class
from the existing class
• In doing this, you can reuse the fields and
methods of the existing class without having to
write them yourself
Inheritance
• A subclass inherits all the members (fields,
methods, and nested classes) from its
superclass
• Constructors are not members, so they are not
inherited by subclasses, but the constructor of
the superclass can be invoked from the subclass
Inheritance
• At the top of the hierarchy, Object is the most
general of all classes. Classes near the bottom
of the hierarchy provide more specialized
behavior
Inheritance
public class Bicycle { // PARENT or SUPERCLASS
// the Bicycle class has three fields
private int cadence;
private int gear;
private int speed;
// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear; cadence = startCadence; speed = startSpeed;
}
// the Bicycle class has two methods
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
// Setter Getter
}
Inheritance
public class MountainBike extends Bicycle { // CHILD or SUBCLASS
// the MountainBike subclass adds one field
private int seatHeight;
// the MountainBike subclass has one constructor
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
// the MountainBike subclass add one method
public void setHeight(int newValue) {
seatHeight = newValue;
}
}
Inheritance
• Has a is a member of class
Example:
class Vehicle {
int wheel;
}
• In above example, Vehicle has a wheel
Inheritance
• Is a is a relation of class
Example:
class Vehicle { }
class Bike extends Vehicle { }
class Car extends Vehicle { }
• In above example,
Bike is a Vehicle
Car is a Vehicle
Vehicle might be a Car, but not always
Polymorphism
• The dictionary definition of polymorphism refers
to a principle in biology in which an organism or
species can have many different forms or stages
• This principle can also be applied to object-
oriented programming and languages like the
Java language
• Subclasses of a class can define their own
unique behaviors and yet share some of the
same functionality of the parent class
Polymorphism
public class Bicycle { // PARENT or SUPERCLASS
// the Bicycle class has three fields
private int cadence;
private int gear;
private int speed;
// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear; cadence = startCadence; speed = startSpeed;
}
// the Bicycle class has three methods
public void printDescription() {
System.out.println(“Gear=" + gear + " cadence=" + cadence + " and speed=" + speed);
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
// Setter Getter
}
Polymorphism
public class MountainBike extends Bicycle { // CHILD or SUBCLASS
// the MountainBike subclass adds one field
private String suspension;
// the MountainBike subclass has one constructor
public MountainBike(String suspension,int startCadence,int
startSpeed,int startGear){
super(startCadence, startSpeed, startGear);
setSuspension(suspension);
}
public void printDescription() { // Override
super. printDescription();
System.out.println("MountainBike has a" + getSuspension());
}
// Setter Getter
}
Polymorphism
public class RoadBike extends Bicycle { // CHILD or SUBCLASS
// the RoadBike subclass adds one field
private int tireWidth;
// the RoadBike subclass has one constructor
public RoadBike(int tireWidth,int startCadence,int startSpeed,int
startGear){
super(startCadence, startSpeed, startGear);
setTireWidth(tireWidth);
}
public void printDescription() { // Override
super. printDescription();
System.out.println(“RoadBike has a" + getTireWidth());
}
// Setter Getter
}
Polymorphism
public class TestBikes {
public static void main(String[] args) {
Bicycle bike01, bike02, bike03;
bike01 = new Bicycle(20, 10, 1);
bike02 = new MountainBike(20, 10, 5, "Dual");
bike03 = new RoadBike(40, 20, 8, 23);
bike01.printDescription();
bike02.printDescription();
bike03.printDescription();
}
}
Interface
• There are a number of situations in software
engineering when it is important for disparate
groups of programmers to agree to a "contract"
that spells out how their software interacts
• Each group should be able to write their code
without any knowledge of how the other group's
code is written
• Generally speaking, interfaces are such
contracts
Interface
• For example, imagine a futuristic society where
computer-controlled robotic cars transport
passengers through city streets without a human
operator
• Automobile manufacturers write software (Java,
of course) that operates the automobile—stop,
start, accelerate, turn left, and so forth
Interface
• The auto manufacturers must publish an
industry-standard interface that spells out in
detail what methods can be invoked to make the
car move (any car, from any manufacturer)
• The guidance manufacturers can then write
software that invokes the methods described in
the interface to command the car
Interface
• Neither industrial group needs to know how the
other group's software is implemented
• In fact, each group considers its software highly
proprietary and reserves the right to modify it at
any time, as long as it continues to adhere to the
published interface
Interface
public interface OperateCar {
void moveForward();
void applyBrake();
void shiftGear();
void turn();
void signalTurn();
}
Interface
public class Bmw implements OperateCar {
void moveForward() { }
void applyBrake() { }
void shiftGear() { }
void turn() { }
void signalTurn() { }
}
Interface
public class Jeep implements OperateCar {
void moveForward() { }
void applyBrake() { }
void shiftGear() { }
void turn() { }
void signalTurn() { }
}
Abstract Class
• An abstract class is an incomplete class.
• A class that is declared abstract—it may or may
not include abstract methods
• Abstract classes cannot be instantiated, but they
can be subclassed
Abstract Class
• An abstract method is a method that is declared
without an implementation (without braces, and
followed by a semicolon), like this:
abstract void moveTo(int x, int y);
• If a class includes abstract methods, then the
class itself must be declared abstract, as in:
public abstract class GraphicObject {
abstract void draw();
}
Abstract Class
• When an abstract class is subclassed, the
subclass usually provides implementations for
all of the abstract methods in its parent class
• However, if it does not, then the subclass must
also be declared abstract
• Abstract classes are similar to interfaces, we
cannot instantiate them
Abstract Class
• However, with abstract classes, you can declare
fields that are not static and final, and define
public, protected, and private concrete methods
• With interfaces, all fields are automatically
public, static, and final, and all methods that you
declare or define (as default methods) are public
• In addition, you can extend only one class,
whether or not it is abstract, whereas you can
implement any number of interfaces
Abstract Class
• First you declare an abstract
class, GraphicObject, to provide member
variables and methods that are wholly shared by
all subclasses. GraphicObject also declares
abstract methods for such as draw or resize, that
need to be implemented by all subclasses but
must be implemented in different ways
Abstract Class
abstract class GraphicObject {
int x, y;
void moveTo(int newX, int newY) { }
abstract void draw();
abstract void resize();
}
Abstract Class
class Circle extends GraphicObject {
void draw() { }
void resize() { }
}
class Rectangle extends GraphicObject {
void draw() { }
void resize() { }
}
Abstract Class
abstract class X implements Y {
// implements all but one method of Y
}
class XX extends X {
// implements the remaining method in Y
}
Nested Class
• The name "nested class" suggests you just write
a class declaration inside a class
• Actually, there are four different kinds of nested
classes specialized for different purposes, and
given different names
Nested Class
• All classes are either:
– Top-level or nested
– Nested classes are:
• Static classes or inner classes
• Inner classes are:
– Member classes or local classes or anonymous classes
Nested Class
• Represents the hierarchy and terminology
of nested classes in a diagram
Nested Class – Nested Static
• Example:
class Top {
static class MyNested { }
}
• A static nested class acts exactly like a
top-level class
Nested Class – Nested Static
• The only differences are:
– The full name of the nested static class
includes the name of the class in which it is
nested, e.g. Top.MyNested
– Instance declarations of the nested class
outside Top would look like:
Top.MyNested myObj = new Top.MyNested();
– The nested static class has access to all the
static methods and static data of the class it is
nested in, even the private members.
Nested Class – Nested Static
class Top {
int i;
static class MyNested {
Top t = new Top();
{
t.i = 3; // accessing Top data
}
}
}
• This code shows how a static nested class can access instance data
of the class it is nested within
Nested Class – Nested Static
• Where to use a nested static class:
Imagine you are implementing a complicated
class. Halfway through, you realize that you
need a "helper" type with some utility
methods. This helper type is self-contained
enough that it can be a separate class from
the complicated class. It can be used by other
classes, not just the complicated class. But it
is tied to the complicated class. Without the
complicated class, there would be no reason
for the helper class to exist
Nested Class – Nested Static
• Before nested classes, there wasn't a
good solution to this, and you'd end up
solving it by making the helper class a top-
level class, and perhaps make some
members of the complicated class more
public than they should be
• Today, you'd just make the helper class
nested static, and put it inside the
complicated class
Nested Class – Inner Class
• Java supports an instance class being
declared within another class, just as an
instance method or instance data field is
declared within a class
• The three varieties of inner class are
– Member class
– Local class
– Anonymous class
Nested Class – Inner Class – Member Class
• Example:
class Top {
class MyMember { }
}
• Member class will have fields and
methods
Nested Class – Inner Class – Member Class
public class Top {
int i = 10;
class MyMember {
int k = i;
int foo() {
return this.k;
}
}
void doCalc() {
MyMember m1 = new MyMember();
MyMember m2 = new MyMember();
m1.k = 10 * m2.foo();
System.out.println("m1.k = " + m1.k);
System.out.println("m2.k = " + m2.k);
}
public static void main(String[] args) {
Top t = new Top();
t.doCalc();
}
}
Nested Class – Inner Class – Local Class
public class Top {
void doCalc() {
class MyLocal {
int k = 5;
int foo() {
return this.k * 10;
}
}
MyLocal m = new MyLocal();
System.out.println("m.k = " + m.k);
System.out.println("m.foo = " + m.foo());
}
public static void main(String[] args) {
Top t = new Top();
t.doCalc();
}
}
Nested Class – Inner Class – Anonymous Class
public abstract class MyAbstract {
public abstract void print();
}
public class MyAnonymous {
public static void main(String[] args) {
MyAbstract m = new MyAbstract() {
@override
public void print() {
System.out.println(“TEST”);
}
}
}
}
Nested Class – Inner Class – Anonymous Class
public class MyAnonymous {
public void print() {
System.out.println(“PRINT IN ANONYMOUS”);
}
}
public class Test {
public static void main(String[] args) {
MyAnonymous m = new MyAnonymous() {
public void print() {
System.out.println(“PRINT IN MAIN”);
}
};
m.print();
}
}
JAR
• JAR = Java Archive
• JAR = Zip File
• To make JAR running, we create a manifest
which is the main class to start
JAR
• How to make JAR:
1. Right click in root project
2. Click “Export”
3. Choose “Runnable JAR file”
4. Choose the main class and export destination
5. Finish
JAR
• How to make JAR:
JAR
• How to run JAR:
1. Open command prompt
2. Type “java –jar jartest.jar”
Thank You

More Related Content

What's hot

Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna Mitchell
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
ANURAG SINGH
 
Core java
Core javaCore java
Core java
kasaragaddaslide
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
Ram132
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Majid Saeed
 
Oops in java
Oops in javaOops in java
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
Tarunsingh198
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
Nilesh Dalvi
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 

What's hot (17)

Java basic
Java basicJava basic
Java basic
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Core java
Core javaCore java
Core java
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Oops in java
Oops in javaOops in java
Oops in java
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Java Day-4
Java Day-4Java Day-4
Java Day-4
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 

Viewers also liked

enums
enumsenums
enums
teach4uin
 
Curso de Java Intermedio
Curso de Java IntermedioCurso de Java Intermedio
Curso de Java Intermedio
José Angel Vaderrama Noguez
 
Nested and Enum in Java
Nested and Enum in JavaNested and Enum in Java
Nested and Enum in Java
ashishspace
 
Unit 1 AP Bio Vocab
Unit 1 AP Bio VocabUnit 1 AP Bio Vocab
Unit 1 AP Bio Vocab
Andrew Shaw
 
Single Nucleotide Polymorphism Analysis (SNPs)
Single Nucleotide Polymorphism Analysis (SNPs)Single Nucleotide Polymorphism Analysis (SNPs)
Single Nucleotide Polymorphism Analysis (SNPs)
Data Science Thailand
 
Genetic polymorphism
Genetic polymorphismGenetic polymorphism
Genetic polymorphism
Dandu Prasad Reddy
 

Viewers also liked (7)

enums
enumsenums
enums
 
Curso de Java Intermedio
Curso de Java IntermedioCurso de Java Intermedio
Curso de Java Intermedio
 
Nested and Enum in Java
Nested and Enum in JavaNested and Enum in Java
Nested and Enum in Java
 
Unit 1 AP Bio Vocab
Unit 1 AP Bio VocabUnit 1 AP Bio Vocab
Unit 1 AP Bio Vocab
 
Single Nucleotide Polymorphism Analysis (SNPs)
Single Nucleotide Polymorphism Analysis (SNPs)Single Nucleotide Polymorphism Analysis (SNPs)
Single Nucleotide Polymorphism Analysis (SNPs)
 
Genetic polymorphism
Genetic polymorphismGenetic polymorphism
Genetic polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Similar to 04 Java Language And OOP Part IV

Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
Chandan Gupta Bhagat
 
Java for beginners
Java for beginnersJava for beginners
Java for beginners
Saeid Zebardast
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
Partnered Health
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
Purvik Rana
 
Java Basics
Java BasicsJava Basics
Java Basics
Rajkattamuri
 
Cse java
Cse javaCse java
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
VijalJain3
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
SivaSankari36
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
Nurhanna Aziz
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
Bui Kiet
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
SamuelAnsong6
 
Java Basics
Java BasicsJava Basics
Java Basics
F K
 
Core java
Core javaCore java
Core java
Shivaraj R
 
Core java
Core javaCore java
Core java
Sun Technlogies
 
Core java
Core javaCore java
Core java
Rajkattamuri
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classes
mcollison
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
3java Advanced Oop
3java Advanced Oop3java Advanced Oop
3java Advanced OopAdil Jafri
 

Similar to 04 Java Language And OOP Part IV (20)

Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
 
Java for beginners
Java for beginnersJava for beginners
Java for beginners
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
 
Oops concept
Oops conceptOops concept
Oops concept
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Cse java
Cse javaCse java
Cse java
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Core java
Core javaCore java
Core java
 
Core java
Core javaCore java
Core java
 
Core java
Core javaCore java
Core java
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classes
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
3java Advanced Oop
3java Advanced Oop3java Advanced Oop
3java Advanced Oop
 

More from Hari Christian

HARI CV AND RESUME
HARI CV AND RESUMEHARI CV AND RESUME
HARI CV AND RESUME
Hari Christian
 
01 Java Language And OOP Part I LAB
01 Java Language And OOP Part I LAB01 Java Language And OOP Part I LAB
01 Java Language And OOP Part I LAB
Hari Christian
 
02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB
Hari Christian
 
DB2 Sql Query
DB2 Sql QueryDB2 Sql Query
DB2 Sql Query
Hari Christian
 
02 Java Language And OOP PART II
02 Java Language And OOP PART II02 Java Language And OOP PART II
02 Java Language And OOP PART II
Hari Christian
 
MM38 kelas B Six Sigma
MM38 kelas B Six SigmaMM38 kelas B Six Sigma
MM38 kelas B Six Sigma
Hari Christian
 

More from Hari Christian (6)

HARI CV AND RESUME
HARI CV AND RESUMEHARI CV AND RESUME
HARI CV AND RESUME
 
01 Java Language And OOP Part I LAB
01 Java Language And OOP Part I LAB01 Java Language And OOP Part I LAB
01 Java Language And OOP Part I LAB
 
02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB
 
DB2 Sql Query
DB2 Sql QueryDB2 Sql Query
DB2 Sql Query
 
02 Java Language And OOP PART II
02 Java Language And OOP PART II02 Java Language And OOP PART II
02 Java Language And OOP PART II
 
MM38 kelas B Six Sigma
MM38 kelas B Six SigmaMM38 kelas B Six Sigma
MM38 kelas B Six Sigma
 

Recently uploaded

Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Yara Milbes
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 

Recently uploaded (20)

Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 

04 Java Language And OOP Part IV

  • 1. Java Language and OOP Part IV By Hari Christian
  • 2. Agenda • 01 Enum • 02 Final • 03 Static • 04 Variable Args • 05 Encapsulation • 06 Inheritance • 07 Polymorphism • 08 Interfaces • 09 Abstract Class • 10 Nested class • 11 JAR
  • 3. Enum • Old approach class Bread { static final int wholewheat = 0; static final int ninegrain = 1; static final int rye = 2; static final int french = 3; } int todaysLoaf = Bread.rye;
  • 4. Enum • New approach public enum Bread { wholewheat, ninegrain, rye, french } Bread todaysLoaf = Bread.rye;
  • 5. Enum • Enum with Constructor public enum Egg { small(10), medium(20), large(30); Egg(int size) { this.size = size; } private int size; // Setter Getter }
  • 6. Enum • Enum with Constructor public enum JobCategory { staff(“Staff”), nonstaff(“Non Staff”); JobCategory(String name) { this.name = name; } private String name; // Setter Getter }
  • 7. Final • When a reference variable is declared final, it means that you cannot change that variable to point at some other object. • You can, however, access the variable and change its fields through that final reference variable. • The reference is final, not the referenced object.
  • 8. Final • Example: void someMethod(final MyClass c, final int a[]) { c.field = 7; // allowed a[0] = 7; // allowed c = new MyClass(); // NOT allowed a = new int[13]; // NOT allowed }
  • 9. Static • Field which is only one copy • Also called class variable • What you can make static: 1. Fields 2. Methods 3. Blocks 4. Class
  • 10. Static - Fields • Example: class Employee { int id; // per-object field int salary; // per-object field static int total; // per-class field (one only) }
  • 11. Static - Fields • Example: class EmployeeTest { public static void main(String[] args) { Employee h = new Employee(); h.id = 1; Employee r = new Employee(); r.id = 2; Employee.total = 10; h.total; // 10 r.total; // 10 } }
  • 12. Static - Methods • Also called class method • Example: public static int parseInt(String s) { // statements go here. } int i = Integer.parseInt("2048");
  • 13. Variable Arity – Var Args • Var Args is optional • Var Args is an Array
  • 14. Var Args public class VarArgs { public static void main(String[] args) { sum(); sum(1, 2, 3, 4, 5); } public static void sum(int … numbers) { int total = 0; for (int i = 0; i < numbers.length; i++) { total += numbers[i]; } return total; }
  • 15. Encapsulation • Imagine if you made your class with public instance variables, and those other programmers were setting the instance variables directly public class BadOO { public int size; } public class ExploitBadOO { public static void main (String [] args) { BadOO b = new BadOO(); b.size = -5; // Legal but bad!! }
  • 16. Encapsulation • OO good design is hide the implementation detail by using Encapsulation • How do you do that? – Keep instance variables protected (with an access modifier, often private) – Make public accessor methods, and force calling code to use those methods rather than directly accessing the instance variable – For the methods, use the JavaBeans naming convention of set<someProperty> and get<someProperty>
  • 17. Encapsulation • Rewrite the class public class GoodOO { private int size; public void setSize(int size) { if (size < 0) this.size = 0; // does not accept negative else this.size = size; } public void getSize() { return size; } } public class ExploitGoodOO { public static void main (String [] args) { GoodOO g = new GoodOO(); g.setSize(-5); // it’s safe now, no need to worry }
  • 19. Inheritance • A class that is derived from another class is called a subclass (also a derived class, extended class, or child class) • The class from which the subclass is derived is called a superclass (also a base class or a parent class)
  • 20. Inheritance • Every class has one and only one direct superclass (single inheritance), Object • Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object
  • 21. Inheritance • The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class • In doing this, you can reuse the fields and methods of the existing class without having to write them yourself
  • 22. Inheritance • A subclass inherits all the members (fields, methods, and nested classes) from its superclass • Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass
  • 23. Inheritance • At the top of the hierarchy, Object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior
  • 24. Inheritance public class Bicycle { // PARENT or SUPERCLASS // the Bicycle class has three fields private int cadence; private int gear; private int speed; // the Bicycle class has one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has two methods public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } // Setter Getter }
  • 25. Inheritance public class MountainBike extends Bicycle { // CHILD or SUBCLASS // the MountainBike subclass adds one field private int seatHeight; // the MountainBike subclass has one constructor public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } // the MountainBike subclass add one method public void setHeight(int newValue) { seatHeight = newValue; } }
  • 26. Inheritance • Has a is a member of class Example: class Vehicle { int wheel; } • In above example, Vehicle has a wheel
  • 27. Inheritance • Is a is a relation of class Example: class Vehicle { } class Bike extends Vehicle { } class Car extends Vehicle { } • In above example, Bike is a Vehicle Car is a Vehicle Vehicle might be a Car, but not always
  • 28. Polymorphism • The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages • This principle can also be applied to object- oriented programming and languages like the Java language • Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class
  • 29. Polymorphism public class Bicycle { // PARENT or SUPERCLASS // the Bicycle class has three fields private int cadence; private int gear; private int speed; // the Bicycle class has one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has three methods public void printDescription() { System.out.println(“Gear=" + gear + " cadence=" + cadence + " and speed=" + speed); } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } // Setter Getter }
  • 30. Polymorphism public class MountainBike extends Bicycle { // CHILD or SUBCLASS // the MountainBike subclass adds one field private String suspension; // the MountainBike subclass has one constructor public MountainBike(String suspension,int startCadence,int startSpeed,int startGear){ super(startCadence, startSpeed, startGear); setSuspension(suspension); } public void printDescription() { // Override super. printDescription(); System.out.println("MountainBike has a" + getSuspension()); } // Setter Getter }
  • 31. Polymorphism public class RoadBike extends Bicycle { // CHILD or SUBCLASS // the RoadBike subclass adds one field private int tireWidth; // the RoadBike subclass has one constructor public RoadBike(int tireWidth,int startCadence,int startSpeed,int startGear){ super(startCadence, startSpeed, startGear); setTireWidth(tireWidth); } public void printDescription() { // Override super. printDescription(); System.out.println(“RoadBike has a" + getTireWidth()); } // Setter Getter }
  • 32. Polymorphism public class TestBikes { public static void main(String[] args) { Bicycle bike01, bike02, bike03; bike01 = new Bicycle(20, 10, 1); bike02 = new MountainBike(20, 10, 5, "Dual"); bike03 = new RoadBike(40, 20, 8, 23); bike01.printDescription(); bike02.printDescription(); bike03.printDescription(); } }
  • 33. Interface • There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts • Each group should be able to write their code without any knowledge of how the other group's code is written • Generally speaking, interfaces are such contracts
  • 34. Interface • For example, imagine a futuristic society where computer-controlled robotic cars transport passengers through city streets without a human operator • Automobile manufacturers write software (Java, of course) that operates the automobile—stop, start, accelerate, turn left, and so forth
  • 35. Interface • The auto manufacturers must publish an industry-standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer) • The guidance manufacturers can then write software that invokes the methods described in the interface to command the car
  • 36. Interface • Neither industrial group needs to know how the other group's software is implemented • In fact, each group considers its software highly proprietary and reserves the right to modify it at any time, as long as it continues to adhere to the published interface
  • 37. Interface public interface OperateCar { void moveForward(); void applyBrake(); void shiftGear(); void turn(); void signalTurn(); }
  • 38. Interface public class Bmw implements OperateCar { void moveForward() { } void applyBrake() { } void shiftGear() { } void turn() { } void signalTurn() { } }
  • 39. Interface public class Jeep implements OperateCar { void moveForward() { } void applyBrake() { } void shiftGear() { } void turn() { } void signalTurn() { } }
  • 40. Abstract Class • An abstract class is an incomplete class. • A class that is declared abstract—it may or may not include abstract methods • Abstract classes cannot be instantiated, but they can be subclassed
  • 41. Abstract Class • An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: abstract void moveTo(int x, int y); • If a class includes abstract methods, then the class itself must be declared abstract, as in: public abstract class GraphicObject { abstract void draw(); }
  • 42. Abstract Class • When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class • However, if it does not, then the subclass must also be declared abstract • Abstract classes are similar to interfaces, we cannot instantiate them
  • 43. Abstract Class • However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods • With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public • In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces
  • 44. Abstract Class • First you declare an abstract class, GraphicObject, to provide member variables and methods that are wholly shared by all subclasses. GraphicObject also declares abstract methods for such as draw or resize, that need to be implemented by all subclasses but must be implemented in different ways
  • 45. Abstract Class abstract class GraphicObject { int x, y; void moveTo(int newX, int newY) { } abstract void draw(); abstract void resize(); }
  • 46. Abstract Class class Circle extends GraphicObject { void draw() { } void resize() { } } class Rectangle extends GraphicObject { void draw() { } void resize() { } }
  • 47. Abstract Class abstract class X implements Y { // implements all but one method of Y } class XX extends X { // implements the remaining method in Y }
  • 48. Nested Class • The name "nested class" suggests you just write a class declaration inside a class • Actually, there are four different kinds of nested classes specialized for different purposes, and given different names
  • 49. Nested Class • All classes are either: – Top-level or nested – Nested classes are: • Static classes or inner classes • Inner classes are: – Member classes or local classes or anonymous classes
  • 50. Nested Class • Represents the hierarchy and terminology of nested classes in a diagram
  • 51. Nested Class – Nested Static • Example: class Top { static class MyNested { } } • A static nested class acts exactly like a top-level class
  • 52. Nested Class – Nested Static • The only differences are: – The full name of the nested static class includes the name of the class in which it is nested, e.g. Top.MyNested – Instance declarations of the nested class outside Top would look like: Top.MyNested myObj = new Top.MyNested(); – The nested static class has access to all the static methods and static data of the class it is nested in, even the private members.
  • 53. Nested Class – Nested Static class Top { int i; static class MyNested { Top t = new Top(); { t.i = 3; // accessing Top data } } } • This code shows how a static nested class can access instance data of the class it is nested within
  • 54. Nested Class – Nested Static • Where to use a nested static class: Imagine you are implementing a complicated class. Halfway through, you realize that you need a "helper" type with some utility methods. This helper type is self-contained enough that it can be a separate class from the complicated class. It can be used by other classes, not just the complicated class. But it is tied to the complicated class. Without the complicated class, there would be no reason for the helper class to exist
  • 55. Nested Class – Nested Static • Before nested classes, there wasn't a good solution to this, and you'd end up solving it by making the helper class a top- level class, and perhaps make some members of the complicated class more public than they should be • Today, you'd just make the helper class nested static, and put it inside the complicated class
  • 56. Nested Class – Inner Class • Java supports an instance class being declared within another class, just as an instance method or instance data field is declared within a class • The three varieties of inner class are – Member class – Local class – Anonymous class
  • 57. Nested Class – Inner Class – Member Class • Example: class Top { class MyMember { } } • Member class will have fields and methods
  • 58. Nested Class – Inner Class – Member Class public class Top { int i = 10; class MyMember { int k = i; int foo() { return this.k; } } void doCalc() { MyMember m1 = new MyMember(); MyMember m2 = new MyMember(); m1.k = 10 * m2.foo(); System.out.println("m1.k = " + m1.k); System.out.println("m2.k = " + m2.k); } public static void main(String[] args) { Top t = new Top(); t.doCalc(); } }
  • 59. Nested Class – Inner Class – Local Class public class Top { void doCalc() { class MyLocal { int k = 5; int foo() { return this.k * 10; } } MyLocal m = new MyLocal(); System.out.println("m.k = " + m.k); System.out.println("m.foo = " + m.foo()); } public static void main(String[] args) { Top t = new Top(); t.doCalc(); } }
  • 60. Nested Class – Inner Class – Anonymous Class public abstract class MyAbstract { public abstract void print(); } public class MyAnonymous { public static void main(String[] args) { MyAbstract m = new MyAbstract() { @override public void print() { System.out.println(“TEST”); } } } }
  • 61. Nested Class – Inner Class – Anonymous Class public class MyAnonymous { public void print() { System.out.println(“PRINT IN ANONYMOUS”); } } public class Test { public static void main(String[] args) { MyAnonymous m = new MyAnonymous() { public void print() { System.out.println(“PRINT IN MAIN”); } }; m.print(); } }
  • 62. JAR • JAR = Java Archive • JAR = Zip File • To make JAR running, we create a manifest which is the main class to start
  • 63. JAR • How to make JAR: 1. Right click in root project 2. Click “Export” 3. Choose “Runnable JAR file” 4. Choose the main class and export destination 5. Finish
  • 64. JAR • How to make JAR:
  • 65. JAR • How to run JAR: 1. Open command prompt 2. Type “java –jar jartest.jar”

Editor's Notes

  1. Bike is in gear 1 with a cadence of 20 and travelling at a speed of 10. Bike is in gear 5 with a cadence of 20 and travelling at a speed of 10. The MountainBike has a Dual suspension. Bike is in gear 8 with a cadence of 40 and travelling at a speed of 20. The RoadBike has 23 MM tires.