java class 3

26
Java Review (Packages)

Upload: edureka

Post on 01-Dec-2014

612 views

Category:

Education


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Java class 3

Java Review (Packages)

Page 2: Java class 3

Java Programming: OOP 2

What is Package?

• A Java package is a mechanism for organizing Java classes into namespaces

• Programmers use packages to organize classes belonging to the same category

• Classes in the same package can access each other's package-access members

Page 3: Java class 3

Java Programming: OOP 3

Advantage of a Package

• Programmers can easily determine that these classes are related

• Programmers know where to find files of similar types

• The names won't conflict• You can have define access of the types within the

package

Page 4: Java class 3

Java Programming: OOP 4

Naming Convention of a Package

• Package names are written in all lower case

• Companies use their reversed Internet domain name to begin their package names—

for example, com.example.mypackage for a package named mypackage created by a programmer at example.com

Page 5: Java class 3

Java Programming: OOP 5

Naming Convention of a Package

Legalizing Package Names

Domain Name Package Name Prefix

hyphenated-name.example.org org.example.hyphenated_name

example.int int_.example

123name.example.com com.example._123name

If the domain name contains - i. a hyphen or a special character ii. if the package name begins with a digit, illegal character reserved Java

keyword such as "int“ In this event, the suggested convention is to add an underscore

Page 6: Java class 3

Java Programming: OOP 6

Package

package com.myproject;ProjectProject

classesclasses

comcom

myprojectmyproject

MyClass.classMyClass.class

MyClass.javaMyClass.java

myprojectmyproject

comcom

srcsrc

Page 7: Java class 3

Inheritance

Page 8: Java class 3

Java Programming: OOP 8

• The child classes inherits all the attributes of the parent class

• They also have their distinctive attributes

Child Class

Super Class Animal

Reptiles Amphibian Mammal

Page 9: Java class 3

Java Programming: OOP 9

Class Animal {// Type : Not Human// Lives : On Land// Gives Birth :}

Class Aquatic extends Animal {// Lives : In Water// Gives Birth : }

Page 10: Java class 3

Java Programming: OOP 10

package com.edureka.animal;

public class Animal {

public String Type = "Not Human";public String Lives = "In Water";

public void fun(){

}

}

package com.edureka.animal;

public class Aquatic extends Animal{

String Lives = "In Water";

public void fun(){ System.out.println("defined here"); }}

Page 11: Java class 3

Java Programming: OOP 11

package com.edureka.animal;

public class Main {

public static void main(String[] args) {

Aquatic ob = new Aquatic(); System.out.println(ob.Type); System.out.println(ob.Lives); ob.fun(); }}

Page 12: Java class 3

Java Methods & Classes

Page 13: Java class 3

Java Programming: OOP 13

Java OOP

• Create new object type with class keyword.

• A class definition can contain:– variables (fields)– initialization code– methods

Page 14: Java class 3

An Example class

package com.edureka.entity; // package

public class Car{ //class declaration String name;

String color;float weight;

...public void move(){ // method

...}

}

Page 15: Java class 3

Objects

• Runtime representation of a class.• Objects hold state with variables• Objects do some work with methods.• Can be created during Runtime as follows : Car carRef = new Car();

carRef New Car()

Newly created ObjectObject Reference

Page 16: Java class 3

Methods

• A method is a collection of statements that are grouped together to perform an operation.

• A method takes some parameters, performs some computations and then optionally returns a value (or object).

Page 17: Java class 3

return_type method_name (arg1, arg2, arg3){____ ____

}

Methods

Methods have five components:• Modifiers• The return type• The method name• The parameter list in

parenthesis • The method body, enclosed

between braces

Body of the Method

Any Number of Parameters

Name of the MethodValue to be

returned

Page 18: Java class 3

Method Overloading

• The overloaded function must differ either by the number of arguments or operands or data types.

• The same function name is used for various instances of function call

Page 19: Java class 3

Method Overloading

package com.edureka.animal;

public class Animal {

public void fun(){ System.out.println("Without Parameters");

}

}

package com.edureka.animal;

public class Aquatic extends Animal{

public void fun(int num){ System.out.println ("The number passed is : " + num); }}

Page 20: Java class 3

Method Overloading

package com.edureka.animal;

public class Main {

public static void main(String[] args) {

Aquatic ob = new Aquatic(); //without Parameters, defined in class Animal ob.fun(); //with Parameter, overloaded function in class Aquatic ob.fun(10); }}

Page 21: Java class 3

Method Overriding

• The implementation in the subclass overrides (replaces) the implementation in the superclass

• It is done by providing a method that has same 1. name, same 2. parameters, and same 3. return type as the method in the parent class

Page 22: Java class 3

Method Overriding

package com.edureka.animal;

public class Animal {

int fun(int a, int b){ int c = a + b; return c; }

}

package com.edureka.animal;

public class Aquatic extends Animal{

int fun(int a, int b){ System.out.println (“Sum by super class: " +

super.fun(a, b)); int c = a * b; return c; }}

Page 23: Java class 3

Method Overriding

package com.edureka.animal;

public class Main {

public static void main(String[] args) {

Aquatic ob = new Aquatic(); System.out.println(“Product by derived class : ”+ ob.fun(2,3));

}}

Page 24: Java class 3

Java Programming: OOP 24

Modifiers

• public: any method (in any class) can access the field.

• protected: any method in the same package or any derived class can access the field.

• private: only methods within the class can access the field.

• default is that only methods in the same package can access the field.

Page 25: Java class 3

•Q& A..?

Page 26: Java class 3

Thanks..!