Java Review (Packages)
What is Package?• A Java package is a mechanism for  organizing Java classes into namespaces• Programmers use packages to ...
Advantage of a Package• Programmers can easily determine that these  classes are related• Programmers know where to find f...
Naming Convention of a Package• Package names are written in all lower case• Companies use their reversed Internet  domain...
Naming Convention of a PackageIf the domain name contains -i.     a hyphen or a special characterii.    if the package nam...
Package    Project                                      package com.myproject;                                src         ...
Inheritance
• The child classes inherits all the attributes of the  parent class• They also have their distinctive attributes         ...
Class Animal {      // Type : Not Human      // Lives : On Land      // Gives Birth :      }       Class Aquatic extends A...
package com.edureka.animal;public class Animal {public String Type = "Not Human";                                         ...
package com.edureka.animal;public class Main {public static void main(String[] args) {        Aquatic ob = new Aquatic(); ...
Java Methods & Classes
Java OOP• Create new object type with class keyword.• A class definition can contain:  – variables (fields)  – initializat...
An Example classpackage com.edureka.entity;            // packagepublic class Car{         //class declaration  String nam...
Objects•   Runtime representation of a class.•   Objects hold state with variables•   Objects do some work with methods.• ...
Methods• A method is a collection of statements that are grouped  together to perform an operation.• A method takes some p...
MethodsMethods have five components:• Modifiers                   Value to be                               returned      ...
Method Overloading• The overloaded function must differ either by  the number of arguments or operands or  data types.• Th...
Method Overloadingpackage com.edureka.animal;public class Animal {public void fun(){   System.out.println("Without Paramet...
Method Overloading           package com.edureka.animal;           public class Main {           public static void main(S...
Method Overriding• The implementation in the subclass overrides (replaces) the  implementation in the superclass• It is do...
Method Overridingpackage com.edureka.animal;public class Animal {int fun(int a, int b){        package com.edureka.animal;...
Method Overriding     package com.edureka.animal;     public class Main {     public static void main(String[] args) {    ...
Modifiers• public: any method (in any class) can access the  field.• protected: any method in the same package or any  der...
•Q& A..?
Thanks..!
Upcoming SlideShare
Loading in …5
×

Java class 3

1,195 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,195
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
117
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Demo
  • 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
  • Java class 3

    1. 1. Java Review (Packages)
    2. 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 others package-access members Java Programming: OOP 2
    3. 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 wont conflict• You can have define access of the types within the package Java Programming: OOP 3
    4. 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 Java Programming: OOP 4
    5. 5. Naming Convention of a PackageIf the domain name contains -i. a hyphen or a special characterii. 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 NamesDomain Name Package Name Prefixhyphenated-name.example.org org.example.hyphenated_nameexample.int int_.example123name.example.com com.example._123name Java Programming: OOP 5
    6. 6. Package Project package com.myproject; src com classes myproject com MyClass.java myproject MyClass.class Java Programming: OOP 6
    7. 7. Inheritance
    8. 8. • The child classes inherits all the attributes of the parent class• They also have their distinctive attributes Java Programming: OOP 8
    9. 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. 10. 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
    11. 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(); }} Java Programming: OOP 11
    12. 12. Java Methods & Classes
    13. 13. Java OOP• Create new object type with class keyword.• A class definition can contain: – variables (fields) – initialization code – methods Java Programming: OOP 13
    14. 14. An Example classpackage com.edureka.entity; // packagepublic class Car{ //class declaration String name; String color; float weight; ... public void move(){ // method ... }}
    15. 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. 16. 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).
    17. 17. MethodsMethods 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
    18. 18. 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
    19. 19. Method Overloadingpackage 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); } }
    20. 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. 21. Method Overriding• The implementation in the subclass overrides (replaces) the implementation in the superclass• It is done by providing a method that has same1. name, same2. parameters, and same3. return type as the method in the parent class
    22. 22. Method Overridingpackage 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; } }
    23. 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. 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. Java Programming: OOP 24
    25. 25. •Q& A..?
    26. 26. Thanks..!

    ×