Understanding Object-Oriented Concepts in Java

class a class is a user defined blueprint n.w
1 / 9
Embed
Share

Learn about classes, objects, constructors, packages, object creation methods, anonymous objects, accessors, and mutators in Java programming. Explore the key principles and examples to deepen your understanding of Object-Oriented Programming.

  • Java Programming
  • Object-Oriented Concepts
  • Classes
  • Objects
  • Constructors

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Class: A class is a user defined blueprint or prototype from which objects are created. Object factory Consists of data members, methods, superclasses, interfaces,etc. Object: It is a basic unit of Object Oriented Programming created from classes. Name, State and behaviour Constructors are used for initializing new objects Dynamically allocated on heap

  2. Example: public class Shirt { string color; Shirt(string s){ color = s; } public static void main(String[] args){ Shirt Obj; // reference Obj = new Shirt( red ); System.out.println(Obj.color); } }

  3. String[] args in Java is an array of strings which stores arguments passed by command line while starting a program. All the command line arguments are stored in that array.

  4. JAVA packages Package is a group of classes, sub packages and interfaces. Packages are used for: Preventing naming conflicts Like a directory structure Packages can be considered as data encapsulation (or data-hiding) Providing easy searching of classes and controlled access Example: // import the Vector class from util package. import java.util.vector; // import all the classes from util package which is inside the java package import java.util.*; Built-in Packages: java.io, java.util, java.awt , java.lang , java.net

  5. Ways to create object: 1. Using new keyword: MyClass myObj = new MyClass(); 2. Using Class.forName(String className) method: MyClass myObj = (MyClass)Class.forName( MyClass ).newInstance(); 3. Using clone() method: MyClass myObj = new MyClass(); MyClass myObj1 = (MyClass) myObj.clone();

  6. Anonymous objects These are objects that are instantiated but not stored in a reference. These are used for immediate method calls and are destroyed after the method call. Example :- class Example { public void func() { // some code // } }

  7. Accessors & Mutators: (Getters and Setters) The role of accessors and mutators are to return and set the values of an object's state. way we can set and get values rather than directly exposing fields of a class. As per the OO Principles, one should have properties as private and changes must be allowed through the methods only. This rule is for enforcing the guaranty that there will be no accidental change of property.

  8. A static method is a method that belongs to a class, but its not belongs to an instance of that class and this method can be called without the instance or object of that class. Non-static methods can access any static method and static variable also, without using the object of the class. class Main { public static void main(String[] args) { int n = 3, m = 6; // Java program to call a static method class MyClass { // call the static method int s = MyClass.sum(n, m); // static method public static int sum(int a, int b) { return a + b; } System.out.print("Sum is = " + s); } } }

  9. // Java program to call a non-static method Key points: Much memory is used in non- static methods. In static method, less memory is use for execution because memory allocation happens only once. In static method, the method can only access only static data members and static methods of another class. class MyClass{ public int sum(int a, int b) { return a + b; } } class Main { public static void main(String[] args) { int n = 3, m = 6; MyClass myObj = new MyClass(); int s = myObj.sum(n, m); System.out.print("Sum is = " + s); }

More Related Content