Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming with Java

Similar presentations


Presentation on theme: "Object Oriented Programming with Java"— Presentation transcript:

1 Object Oriented Programming with Java
II MCA(MCA30117),I MSc(CS)(MCS10117)

2 Agenda Introduction to Java Java Features Execution of Java Program
All that is to know about OOPs Features Introduction to Java Java Features Execution of Java Program Classes & Constructors Inheritance and Polymorphism

3 Different Programming Paradigms
Functional/procedural programming: program is a list of instructions to the computer Object-oriented programming program is composed of a collection objects that communicate with each other Different Programming Paradigms flexibility, easing changes to programs easier to learn simpler to develop, maintain and analysize

4 Four Pillars of OOP Abstraction Encapsulation Inheritance Polymorphism

5 History of Java James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June The small team of sun engineers called Green Team. Originally designed for small, embedded systems in electronic appliances like set-top boxes. Firstly, it was called "Greentalk" by James Gosling and file extension was .gt. After that, it was called Oak and was developed as a part of the Green project. Why Oak? Oak is a symbol of strength and choosen as a national tree of many countries like U.S.A., France, Germany, Romania etc. In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.

6 Features of Java It must be "simple, object-oriented, and familiar".
It must be "robust and secure". It must be "architecture-neutral and portable". It must execute with "high performance". It must be "interpreted, threaded, and dynamic".

7 Platform Dependent gcc machine code OS/Hardware Platform Independent
myprog.exe myprog.c machine code C source code OS/Hardware JVM bytecode Java source code myprog.java javac myprog.class OS/Hardware Platform Independent

8 Behaviors is exactly as in C++
Primitive types Note: Primitive type always begin with lower-case int 4 bytes short 2 bytes long 8 bytes byte 1 byte float 4 bytes double 8 bytes char Unicode encoding (2 bytes) boolean {true,false} Behaviors is exactly as in C++

9 Sample Java Programm Hello.java ( compilation creates Hello.class )
class Hello { public static void main(String[] args) { System.out.println(“Hello World !!!”); } } C:\javac Hello.java C:\java Hello ( compilation creates Hello.class ) (Execution on the local JVM)

10 Classes and Objects A class will look like this:
<Access-Modifier> class MyClass { // field, constructor, and method declarations } To instantiate an object we will do: MyClass instance = new MyClass(<constructor params>);

11 Accessibility Options
Four accessibility options: – public – (default) = “package” ** – protected * – private * protected is also accessible by package ** called also “package-private” or “package-friendly” Example: public class Person { private String name; protected java.util.Date birthDate; String id; // default accessibility = package public Person() {} }

12 Static Example: public class Widget {
Static member can be accessed without an instance (same as in C++) Example: public class Widget { static private int counter; static public getCounter() {return counter;} } int number = Widget.getCounter(); Called sometimes “class variable” as opposed to “instance variable”

13 The ‘this’ keyword Example: public class Point { private int x, y;
In Java ‘this’ is a reference to myself (in C++ it is a pointer…) Example: public class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } The ‘this’ keyword is also used to call another constructor of the same class – we will see that later

14 Defining constants Example: public class Thingy {
Though const is a reserved word in Java it's actually not in use! However the final keyword let's you define constants and const variables Example: public class Thingy { public final static doodad = 6; // constant public final id; // constant variable public Thingy(int id) {this.id = id;} // OK // public set(int id) {this.id = id;} // error! }

15 Constructors Examples in following slides…
– Constructors in Java are very similar to C++ – You can overload constructors (like any other method) – A constructor which doesn't get any parameter is called “empty constructor” – You may prefer not to have a constructor at all, in which case it is said that you have by default an “empty constructor” – A constructor can call another constructor of the same class using the ‘this’ keyword – Calling another constructor can be done only as the first instruction of the calling constructor Examples in following slides…

16 Constructors Example 1: public class Person {
String name = ""; // fields can be initialized! Date birthDate = new Date(); public Person() {} // empty constructor public Person(String name, Date birthDate) { this(name); // must be first instruction this.birthDate = birthDate; } public Person(String name) { this.name = name;

17 Constructors Example 2: public class Person { String name = "";
Date birthDate = new Date(); public Person(String name, Date birthDate) { this.name = name; this.birthDate = birthDate; } Person p; // OK p = new Person(); // not good – compilation error

18 Static Initializer Static initializer is a block of instructions performed the first time a class is loaded Static initializer may be useful to perform a one time initializations of static members Example: public class Thingy { static String s; // the block underneath is a static initializer static { s="Hello"; } } Usually static initializer would do a more complex job…

19 Inheritance Some Terms 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). Excepting java.lang.Object, which has no superclass, every class has exactly one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object. A class is said to be descended from all the classes in its inheritance chain stretching back to Object.

20 Inheritance Examples in following slides…
– Class Object is the ancestor base class of all classes in Java – There is no multiple inheritance in Java – Inheritance is always “public” thus type is not stated (no private or protected inheritance as in C++) – Class can implement several interfaces (contracts) – Class can be abstract – Access to base class is done using the super keyword – Constructor may send parameters to its base using the ‘super’ keyword as its first instruction – If the base class does not have an empty constructor then the class is required to pass parameters to its super Examples in following slides…

21 Inheritance Example 1: public class Person { private String name;
public Person(String name) { this.name = name; } // Override toString in class Object public String toString() { return name;

22 Inheritance Example 1 (cont’): public class Employee extends Person {
private Employee manager; public Employee(String name, Employee manager) { super(name); // must be first this.manager = manager; } // Override toString in class Person public String toString() { return super.toString() + (manager!=null? ", reporting to: " + manager : " - I'm the big boss!");

23 Inheritance Example 2: abstract public class Shape {
// private Color line = Color.Black; // private Color fill = Color.White; public Shape() {} /* public Shape(Color line, Color fill) { this.line = line; this.fill = fill; } */ abstract public void draw(); abstract public boolean isPointInside(Point p); }

24 Inheritance Example 2 (cont’): public class Circle extends Shape {
private Point center; private double radius; public Circle(Point center, double radius) { this.center = center; this.radius = radius; } public void draw() {…} // use Graphics or Graphics2d public boolean isPointInside(Point p) { return (p.distance(center) < radius);

25 of course, final and abstract don‘t go together (why?)
Inheritance The final keyword is used to forbid a method from being override in derived classes Above is relevant when implementing a generic algorithm in the base class, and it allows the JVM to linkage the calls to the method more efficiently The final keyword can also be used on a class to prevent the class from being subclassed at all Example: abstract public class Shape { final public void setFillColor(Color color) {<some implementation>} } of course, final and abstract don‘t go together (why?)

26 Interfaces Examples in following slides…
– Interface is a contract – An interface can contain method signatures (methods without implementation) and static constants – Interface cannot be instantiated, it can only be implemented by classes and extended by other interfaces – Interface that do not include any method signature is called a marker interface – Class can implement several interfaces (contracts) – Class can announce on implementing an interface, without really implementing all of the declared methods, but then the class must be abstract Examples in following slides…

27 Interfaces Example 1 – using interface Comparable:
// a generic max function static public Object max(Comparable... comparables) { int length = comparables.length; if(length == 0) { return null; } Comparable max = comparables[0]; for(int i=1; i<length; i++) { if(max.compareTo(comparables[i]) < 0) { max = comparables[i]; } return max; // calling the function can go like this: String maxStr = (String) max("hello", "world", "!!!");

28 Interfaces Example 2 – supporting foreach on our own type:
To have your own class support iterating, using the "foreach“ syntax, the class should implement the interface Iterable: public interface Iterable<T> { Iterator<T> iterator(); } // example public interface Collection<E> extends Iterable<E> { Iterator<E> iterator(); Exercise: Write class NewString that will allow iterating over its chars using "foreach"

29 Interfaces Example 3 – supporting clone on our own type:
To have your own class support the clone method the class should implement the marker interface Cloneable: public interface Cloneable {} Exercise: Implement clone for class Person

30 Interfaces Example 4 – new IHaveName interface:
To allow name investigation we want to create a new IHaveName interface: public interface IHaveName { String getName(); } Exercise: Create the IHaveName interface and let class Person implement it

31


Download ppt "Object Oriented Programming with Java"

Similar presentations


Ads by Google