Object Oriented Programming with Java

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 10 Classes Continued
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
© Amir Kirsh Object Oriented Programming with Java Written by Amir Kirsh.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Peyman Dodangeh Sharif University of Technology Fall 2014.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Intro to Java L. Grewe.
Inheritance ITI1121 Nour El Kadri.
Unit-1 JAVA Programming.
Chapter 11 Inheritance and Polymorphism
Chapter No. : 1 Introduction to Java.
Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
Internet and Java Foundations, Programming and Practice
Advanced Programming in Java
Programming Language Concepts (CIS 635)
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
Object Oriented Programming with Java
Interface.
Java Programming Language
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Java History, Editions, Version Features
Chapter 14 Abstract Classes and Interfaces
Java Programming Language
Chapter 11 Inheritance and Polymorphism Part 1
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Chapter 7 Objects and Classes
Chengyu Sun California State University, Los Angeles
Presentation transcript:

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

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

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

Four Pillars of OOP Abstraction Encapsulation Inheritance Polymorphism

History of Java James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. 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.

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".

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

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++

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)

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>);

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() {} }

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”

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

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! }

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…

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;

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

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…

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.

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…

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;

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!");

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); }

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);

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?)

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…

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", "!!!");

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"

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

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