Java Review (Packages)
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 Java Programming: OOP 2
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 Java Programming: OOP 3
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 Java Programming: OOP 4
Naming Convention of a Package 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 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 Java Programming: OOP 5
Package Project package com.myproject; src com classes myproject com MyClass.java myproject MyClass.class Java Programming: OOP 6
Inheritance
• The child classes inherits all the attributes of the parent class • They also have their distinctive attributes Java Programming: OOP 8
Class Animal { // Type : Not Human // Lives : On Land // Gives Birth : } Class Aquatic extends Animal { // Lives : In Water // Gives Birth : } Java Programming: OOP 9
package com.edureka.animal; public class Animal { public String Type = "Not Human"; package com.edureka.animal; public String Lives = "In Water"; public class Aquatic extends Animal{ public void fun(){ String Lives = "In Water"; } public void fun(){ } System.out.println("defined here"); } } Java Programming: OOP 10
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(); } } Java Programming: OOP 11
Java Methods & Classes
Java OOP • Create new object type with class keyword. • A class definition can contain: – variables (fields) – initialization code – methods Java Programming: OOP 13
An Example class package com.edureka.entity; // package public class Car{ //class declaration String name; String color; float weight; ... public void move(){ // method ... } }
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() Object Reference Newly created Object
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).
Methods Methods have five components: • Modifiers Value to be returned Name of the Method • The return type return_type method_name (arg1, arg2, arg3) • The method name { ____ Body of the • The parameter list in ____ Method parenthesis } Any Number of Parameters • The method body, enclosed between braces
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
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); } }
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); } }
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
Method Overriding package com.edureka.animal; public class Animal { int fun(int a, int b){ package com.edureka.animal; int c = a + b; return c; 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; } }
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)); } }
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. Java Programming: OOP 24
•Q& A..?
Thanks..!

Java class 3

  • 1.
  • 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 Java Programming: OOP 2
  • 3.
    Advantage of aPackage • 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 Java Programming: OOP 3
  • 4.
    Naming Convention ofa 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 Java Programming: OOP 4
  • 5.
    Naming Convention ofa Package 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 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 Java Programming: OOP 5
  • 6.
    Package Project package com.myproject; src com classes myproject com MyClass.java myproject MyClass.class Java Programming: OOP 6
  • 7.
  • 8.
    • The childclasses inherits all the attributes of the parent class • They also have their distinctive attributes Java Programming: OOP 8
  • 9.
    Class Animal { // Type : Not Human // Lives : On Land // Gives Birth : } Class Aquatic extends Animal { // Lives : In Water // Gives Birth : } Java Programming: OOP 9
  • 10.
    package com.edureka.animal; public classAnimal { public String Type = "Not Human"; package com.edureka.animal; public String Lives = "In Water"; public class Aquatic extends Animal{ public void fun(){ String Lives = "In Water"; } public void fun(){ } System.out.println("defined here"); } } Java Programming: OOP 10
  • 11.
    package com.edureka.animal; public classMain { public static void main(String[] args) { Aquatic ob = new Aquatic(); System.out.println(ob.Type); System.out.println(ob.Lives); ob.fun(); } } Java Programming: OOP 11
  • 12.
  • 13.
    Java OOP • Createnew object type with class keyword. • A class definition can contain: – variables (fields) – initialization code – methods Java Programming: OOP 13
  • 14.
    An Example class packagecom.edureka.entity; // package public class Car{ //class declaration String name; String color; float weight; ... public void move(){ // method ... } }
  • 15.
    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() Object Reference Newly created Object
  • 16.
    Methods • A methodis 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).
  • 17.
    Methods Methods have fivecomponents: • Modifiers Value to be returned Name of the Method • The return type return_type method_name (arg1, arg2, arg3) • The method name { ____ Body of the • The parameter list in ____ Method parenthesis } Any Number of Parameters • The method body, enclosed between braces
  • 18.
    Method Overloading • Theoverloaded 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
  • 19.
    Method Overloading package com.edureka.animal; publicclass 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); } }
  • 20.
    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); } }
  • 21.
    Method Overriding • Theimplementation 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
  • 22.
    Method Overriding package com.edureka.animal; publicclass Animal { int fun(int a, int b){ package com.edureka.animal; int c = a + b; return c; 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; } }
  • 23.
    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)); } }
  • 24.
    Modifiers • public: anymethod (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. Java Programming: OOP 24
  • 25.
  • 26.

Editor's Notes

  • #15 Demo
  • #25 Public/PrivateMethods/data may be declared public or private meaning they may or may not be accessed by code in other classes …Good practice:keep data privatekeep most methods privatewell-defined interface between classes - helps to eliminate errors