SlideShare a Scribd company logo
Closer look at classes
Overloading Methods
Overloading Constructor
Objects as parameter to methods
Objects as parameter to constructor
Returning Objects
Recursion
String Class
String Buffer Class
Command line arguments
Access Controle
Static Keyword Usage
Final Keyword Usage
Overloading methods
• Overloading is ability of one function to perform different tasks.
• it allows creating several methods with the same name which differ from each
other in the type of the input and the output
class Demo_class
{ /* having same method name*/
void demo()
{ System.out.println("hello world"); }
void demo(int a)
{ System.out.println("the value of a::"+a); }
public static void main(String[] y)
{ Demo_class d=new Demo_class();
d.demo();
d.demo(10);
}
}
Overloading Constructor
• In addition to overloading normal methods,you can also overload
constructor methods
class Demo_Constructor_overload
{ Demo_Constructor_overload()
{ System.out.println("hello default constructor");
}
Demo_Constructor_overload(String s)
{ System.out.println("hello default constructor 1::"+s);
}
public static void main(String[] a1)
{ Demo_Constructor_overload d=new Demo_Constructor_overload();
Demo_Constructor_overload d1=new Demo_Constructor_overload
("yugandhar");
}
}
Uses of method Overloading
• The main advantage of this is cleanliness of
code.
• the use of function overloading is to save the
memory space, consistency and readabiliy.
Objects as parameters to methods
class Rectangle
{ int lenght,width;
Rectangle(int lenght,int width)
{ this.lenght=lenght;
this.width=width;
}
void print_values(Rectangle r)
{ System.out.println("the value of member length "+r.lenght+"n the
value of member variable width is::"+this.width);
}
}
public class Demo_object_paramter
{ public static void main(String[] y)
{ Rectangle r=new Rectangle(10,20);
r.print_values(r);
}
}
Objects as parameters to Constructor
class Rectangle
{ int lenght,width;
Rectangle(int lenght,int width)
{ this.lenght=lenght;
this.width=width;
}
Rectangle(Rectangle r)
{ System.out.println("the member varialbe lenght::"+r.lenght+"n the
member varialbe height::"+r.width);
}
}
public class Demo_object_paramter
{ public static void main(String[] y)
{ Rectangle r=new Rectangle(30,20);
Rectangle r1=new Rectangle(r);
}
}
Returning objects
• A method can return any type of data,including class types that you create
class Rectangle
{ int length,width;
Rectangle demo(int length,int width)
{ this.length=length;
this.width=width;
return(this);
}
}
public class Demo_object_paramter
{ public static void main(String[] y)
{ Rectangle r=new Rectangle();
Rectangle r1=r.demo(30,40);
System.out.println("the value of length is::"+r1.length+"n the value of
width is::"+r1.width);
}
}
Recursion
• Recursion is a basic programming technique you can use in Java, in which a
method calls itself to solve some problem.
• A method that uses this technique is recursive.
class Factorial
{ int fact(int n)
{ int result;
if(n==1)
return 1;
result=fact(n-1)*n;
return result;
}
}
public class Recursion_1
{ public static void main(String[] y)
{ Factorial f=new Factorial();
System.out.println("the factorial value is::"+f.fact(5));
}
}
String Class
• The first thing to understand about strings is that every string you create is
actually an object of type string . Every string constant is actually a string
Object.
• Eg System.out.println(“hello world”);
• The secound thing to understand about strings is that objects of type
String are immutable once string objects are created ,its content can not
be altered.
public class Private_AccessDemo
{ public static void main(String[] y)
{ String s=new String("yugandhar");
System.out.println("the s String is::"+s);
System.out.println("the String length::"+s.length());
String s1=new String("programmer");
System.out.println("the String concatination "+s+s1);
}
}
Declaring String Array
public static void main(String[] y)
{ String[] s=new String[3];//string array with 3
Scanner sc=new Scanner(System.in);//taking input at run time from user
for(int i=0;i<s.length;i++)
s[i]=sc.nextLine();
for(int i=0;i<s.length;i++)
System.out.println(" "+s[i]);
}
Some String Methods
public class Private_AccessDemo
{ public static void main(String[] y)
{ String s="yugandhar";
System.out.println("the upper case::"+s.toUpperCase());
System.out.println("the upper case::"+s.toLowerCase());
System.out.println("the replace ::"+s.replace('a','r'));
System.out.println("the charAt ::"+s.charAt(8));
}
}
String Buffer Class
• String Buffer creates the string of flexible length that can be modified in
terms content and length. We can insert characters and substrings in the
middle of the string are append another string at the end.
public class Private_AccessDemo
{ public static void main(String[] y)
{ StringBuffer s=new StringBuffer("yugandhar");//StringBuffer constructor
System.out.println("the String Buffer class");
s.setCharAt(6,'x');//methods of String Buffer class
System.out.println(s);
System.out.println("the append string::"+s.append(" programmer"));
s.setLength(40);// methods of String Buffer class
System.out.println("the set length ::"+s.length());
}
}
Command line arguments
• A command-line argument is the information that directly follows the
program’s name on the command line when it is excuted .
• All command line arguments are passed as strings
public static void main(String[] y)
{ System.out.println("the commandline arguments");
for(int i=0;i<y.length;i++)
System.out.println("the y["+i+"]::"+y[i]);
}
####output####
D:softwaresjava_programs>java Private_AccessDemo h e l l o w
the commandline arguments
the y[0]::h the y[1]::e the y[2]::l the y[3]::l the y[4]::o
the y[5]::w
Access Control
• encapsulation provides another important attribute: access control.
• Java’s access specifiers are public, private, and protected
• Public: A class, method, constructor, interface etc declared public can be
accessed from any other class. Therefore fields, methods, blocks declared
inside a public class can be accessed from any class
• private: Methods, Variables and Constructors that are declared private can
only be accessed within the declared class itself. Private access modifier is
the most restrictive access level. Class and interfaces cannot be private
• protect: Variables, methods and constructors which are declared protected
in a superclass can be accessed only by the subclasses in other package or
any class within the package of the protected members' class. The
protected access modifier cannot be applied to class and interfaces.
• Default: access modifier means we do not explicitly declare an access
modifier for a class, field, method, etc.A variable or method declared
without any access control modifier is available to any other class in the
same package.
Public access specifier
package demo_package;
import java.util.*;
public class AccessDemo
{ public void test()
{ System.out.println("Example of public access specifier"); }
protected int x=200;
}
D:softwaresjava_programs>javac -d . AccessDemo.java
import java.util.*;
import demo_package.AccessDemo;
public class Public_AccessDemo
{ public static void main(String[] y)
{ AccessDemo d=new AccessDemo();
d.test();
}
}
####output####
D:softwaresjava_programs>javac Public_AccessDemo.java
D:softwaresjava_programs>java Public_AccessDemo
Example of public access specifier
Private Access modifyer
class demo
{ private int x=20;
private void demo()
{ System.out.println("the value of x is::"+x);
}
void demo_1()
{ System.out.println("the demo_1 value of x is::"+x);
}
}
public class Private_AccessDemo
{ public static void main(String[] y)
{ demo d=new demo();
System.out.println("the value of x is"+d.x);
d.demo();
d.demo_1();
}
}
####output####
D:softwaresjava_programs>javac Private_AccessDemo.java
error: x has private access in demo
System.out.println("the value of x is"+d.x);
: error: demo() has private access in demo
d.demo()
making the d.demo_1() above two lines as comment then following out put will get
D:softwaresjava_programs>java Private_AccessDemo
the demo_1 value of x is::20
Protected access specifyer
import java.util.*;
import demo_package.AccessDemo;
class demo
{ protected int x=20;
protected void demo()
{ System.out.println("the value of x is::"+x); }
}
public class Private_AccessDemo extends AccessDemo
{ public static void main(String[] y)
{ demo d=new demo(); d.x=30;
System.out.println("the value of x is "+d.x); d.demo();
/*try with this code to know the difference with out extends AccessDemo
AccessDemo a=new AccessDemo();
System.out.println(a.x);
-*/
Private_AccessDemo a=new Private_AccessDemo();
System.out.println(a.x);
}
}
D:softwaresjava_programs>java Private_AccessDemo
the value of x is 30 the value of x is::30 200
Static
Variable declaration with Static
• When a variable is declared with the keyword “static”, its called a “class variable”.
• All instances share the same copy of the variable.
• A class variable can be accessed directly with the class, without the need to create a instance.
class static_
{ static String y="i am static variable";
String y1="hello_world";
}
public class Static_Demo
{ public static void main(String[] y)
{ System.out.println("accessing static variable::"+static_.y);
static_ s=new static_();
s.y="hello";
s.y1="H";
//static_.y="done";
System.out.println("accessing static variable::"+static_.y);
/*
* here y shares the same copy
*/
static_ s1=new static_();
System.out.println("accessing static variable::"+s1.y+"n the value of y1::"+s1.y1);
}
}
Method declaration with static
• It is a method which belongs to the class and not to the object(instance)
• A static method can access only static data. It can not access non-static
data (instance variables)
• A static method can call only other static methods and can not call a non-
static method from it.
• A static method can be accessed directly by the class name and doesn’t
need any object
• Syntax : <class-name>.<method-name>
• A static method cannot refer to “this” or “super” keywords in anyway
class static_
{ int x=10;
static void demo()
{ System.out.println("hellow static method");
// System.out.println("the value of x is"+x);
/*
* static methods can call only other static methods
* and other static variables only
*/
//demo_1();
demo_2();
}
void demo_1()
{ System.out.println("not static method demo_1()"); }
static void demo_2()
{ System.out.println(" static method demo_2()"); }
}//class
public class Static_Demo
{ static void demo_3()
{ System.out.println("the main methods static"); }
public static void main(String[] y)
{ System.out.println("static method");
static_.demo();
demo_3();
}
}
####output####
static method
hellow static method
static method demo_2()
the main methods static
Static block
• The static block, is a block of statement inside a Java class that will be executed when
a class is first loaded in to the JVM.
class static_
{ int x=10;
void demo()
{ System.out.println("hellow"); }
static{ System.out.println("secound static block");
}
static{ System.out.println("first static block");
}
}
public class Static_Demo
{ public static void main(String[] y)
{ static_ s=new static_();
s.demo();
}
}
####output####
secound static block
first static block
hellow
final keyword
• final is a reserved keyword in Java to restrict the user and it can be applied
to member variables, methods, class and local variables.
class Demo
{ final int INT_VARIABLE=120;
/**final variable value can not be chage */
public static void main(String[] y)
{ Demo d=new Demo();
d.INT_VARIABLE=45;
}
}
####output####
C:UsersYugandharDesktopjsp>javac Demo.java
Demo.java:8: error: cannot assign a value to final variable INT_VARIABLE
d.INT_VARIABLE=45;
Final method
• A final method cannot be overridden. Which means even though a sub
class can call the final method of parent class without any issues but it
cannot override it.
class Super
{ final void demo()
{ System.out.println("hello"); }
}
class Sub_ extends Super
{ /*void demo()
{
System.out.println("hello world");
}*/
void demo_1()
{ System.out.println("hello world"); }
}
class Demo
{ public static void main(String[] y)
{ Sub_ s=new Sub_();
s.demo();
}
}
try to remove those comments to understand
####output####
C:UsersYugandharDesktopjsp>javac Demo.java
Demo.java:11: error: demo() in Sub_ cannot override demo() in Super
C:UsersYugandharDesktopjsp>java Demo
hello
Final class
• We cannot extend a final class prevent inheritance
final class Super
{ void demo()
{ System.out.println("hello"); }
}
class Sub_ extends s
{ void demo_1()
{ System.out.println("hello world"); }
}
class Demo
{ public static void main(String[] y)
{ Sub_ s=new Sub_(); }
}
####output####
C:UsersYugandharDesktopjsp>javac Demo.java
Demo.java:9: error: cannot inherit from final Super
class Sub_ extends Super
Uses of final keyword
• Using final to define constants
• Using final to prevent inheritance
• Using final to prevent overriding
• Using final for method arguments

More Related Content

PPTX
Lecture 7 arrays
manish kumar
 
PPTX
20.3 Java encapsulation
Intro C# Book
 
PDF
Object calisthenics
PolSnchezManzano
 
PPTX
Unit3 part2-inheritance
DevaKumari Vijay
 
PDF
C# Starter L04-Collections
Mohammad Shaker
 
PDF
Csharp_Chap03
Mohamed Krar
 
PPTX
20.2 Java inheritance
Intro C# Book
 
PPTX
Operators
vvpadhu
 
Lecture 7 arrays
manish kumar
 
20.3 Java encapsulation
Intro C# Book
 
Object calisthenics
PolSnchezManzano
 
Unit3 part2-inheritance
DevaKumari Vijay
 
C# Starter L04-Collections
Mohammad Shaker
 
Csharp_Chap03
Mohamed Krar
 
20.2 Java inheritance
Intro C# Book
 
Operators
vvpadhu
 

What's hot (18)

PDF
OOPs & Inheritance Notes
Shalabh Chaudhary
 
PPTX
Interface
vvpadhu
 
PDF
C# Summer course - Lecture 3
mohamedsamyali
 
PPTX
Java Programs
vvpadhu
 
PPTX
Unit3 inheritance
Kalai Selvi
 
PDF
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
PPTX
Class object method constructors in java
Raja Sekhar
 
PDF
C# Summer course - Lecture 4
mohamedsamyali
 
PPTX
20.4 Java interfaces and abstraction
Intro C# Book
 
PDF
Java Programming - 05 access control in java
Danairat Thanabodithammachari
 
PPT
14. Defining Classes
Intro C# Book
 
PPT
Introduction to csharp
Raga Vahini
 
PPT
Introduction to csharp
hmanjarawala
 
PPTX
14. Java defining classes
Intro C# Book
 
PPTX
Core java oop
Parth Shah
 
PDF
Introduction To Csharp
sarfarazali
 
PPTX
11. java methods
M H Buddhika Ariyaratne
 
OOPs & Inheritance Notes
Shalabh Chaudhary
 
Interface
vvpadhu
 
C# Summer course - Lecture 3
mohamedsamyali
 
Java Programs
vvpadhu
 
Unit3 inheritance
Kalai Selvi
 
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
Class object method constructors in java
Raja Sekhar
 
C# Summer course - Lecture 4
mohamedsamyali
 
20.4 Java interfaces and abstraction
Intro C# Book
 
Java Programming - 05 access control in java
Danairat Thanabodithammachari
 
14. Defining Classes
Intro C# Book
 
Introduction to csharp
Raga Vahini
 
Introduction to csharp
hmanjarawala
 
14. Java defining classes
Intro C# Book
 
Core java oop
Parth Shah
 
Introduction To Csharp
sarfarazali
 
11. java methods
M H Buddhika Ariyaratne
 
Ad

Viewers also liked (10)

PPTX
Constructor overloading in C++
Learn By Watch
 
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
Abbas Ajmal
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPTX
Constructors & destructors
ForwardBlog Enewzletter
 
PPTX
Overloading in java
774474
 
PPT
constructor and destructor-object oriented programming
Ashita Agrawal
 
PPT
Function overloading(c++)
Ritika Sharma
 
PPTX
Constructor and destructor in c++
Learn By Watch
 
PPT
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
PPTX
Constructor ppt
Vinod Kumar
 
Constructor overloading in C++
Learn By Watch
 
Oop Constructor Destructors Constructor Overloading lecture 2
Abbas Ajmal
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Constructors & destructors
ForwardBlog Enewzletter
 
Overloading in java
774474
 
constructor and destructor-object oriented programming
Ashita Agrawal
 
Function overloading(c++)
Ritika Sharma
 
Constructor and destructor in c++
Learn By Watch
 
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Constructor ppt
Vinod Kumar
 
Ad

Similar to Closer look at classes (20)

PDF
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
PPTX
Hemajava
SangeethaSasi1
 
PPTX
Class and Object.pptx from nit patna ece department
om2348023vats
 
PPTX
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
İbrahim Kürce
 
PPTX
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
PPTX
Object oriented concepts
Gousalya Ramachandran
 
PPTX
Chapter 8 java
Ahmad sohail Kakar
 
PPTX
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
PPT
Java
s4al_com
 
PPT
Explain Classes and methods in java (ch04).ppt
ayaankim007
 
PPT
Md03 - part3
Rakesh Madugula
 
PPTX
‫Object Oriented Programming_Lecture 3
Mahmoud Alfarra
 
PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
shilpar780389
 
PPTX
Presentation2.ppt java basic core ppt .
KeshavMotivation
 
DOCX
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
PDF
7. VARIABLEs presentation in java programming. Pdf
simukondasankananji8
 
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
PPT
Java class
Arati Gadgil
 
PPTX
Imp Key.pptx very easy to learn go for it
dwivedyp
 
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
Hemajava
SangeethaSasi1
 
Class and Object.pptx from nit patna ece department
om2348023vats
 
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
İbrahim Kürce
 
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Object oriented concepts
Gousalya Ramachandran
 
Chapter 8 java
Ahmad sohail Kakar
 
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
Java
s4al_com
 
Explain Classes and methods in java (ch04).ppt
ayaankim007
 
Md03 - part3
Rakesh Madugula
 
‫Object Oriented Programming_Lecture 3
Mahmoud Alfarra
 
UNIT 3- Java- Inheritance, Multithreading.pptx
shilpar780389
 
Presentation2.ppt java basic core ppt .
KeshavMotivation
 
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
7. VARIABLEs presentation in java programming. Pdf
simukondasankananji8
 
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
Java class
Arati Gadgil
 
Imp Key.pptx very easy to learn go for it
dwivedyp
 

More from yugandhar vadlamudi (15)

ODP
Toolbarexample
yugandhar vadlamudi
 
ODP
Singleton pattern
yugandhar vadlamudi
 
PPT
Object Relational model for SQLIite in android
yugandhar vadlamudi
 
DOCX
JButton in Java Swing example
yugandhar vadlamudi
 
PPTX
Collections framework in java
yugandhar vadlamudi
 
PPTX
Packaes & interfaces
yugandhar vadlamudi
 
PPTX
Exception handling in java
yugandhar vadlamudi
 
DOCX
JMenu Creation in Java Swing
yugandhar vadlamudi
 
DOCX
Adding a action listener to button
yugandhar vadlamudi
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
PPTX
Operators in java
yugandhar vadlamudi
 
PPTX
Inheritance
yugandhar vadlamudi
 
PPTX
Control flow statements in java
yugandhar vadlamudi
 
PPTX
java Applet Introduction
yugandhar vadlamudi
 
PPTX
Class introduction in java
yugandhar vadlamudi
 
Toolbarexample
yugandhar vadlamudi
 
Singleton pattern
yugandhar vadlamudi
 
Object Relational model for SQLIite in android
yugandhar vadlamudi
 
JButton in Java Swing example
yugandhar vadlamudi
 
Collections framework in java
yugandhar vadlamudi
 
Packaes & interfaces
yugandhar vadlamudi
 
Exception handling in java
yugandhar vadlamudi
 
JMenu Creation in Java Swing
yugandhar vadlamudi
 
Adding a action listener to button
yugandhar vadlamudi
 
Dynamic method dispatch
yugandhar vadlamudi
 
Operators in java
yugandhar vadlamudi
 
Inheritance
yugandhar vadlamudi
 
Control flow statements in java
yugandhar vadlamudi
 
java Applet Introduction
yugandhar vadlamudi
 
Class introduction in java
yugandhar vadlamudi
 

Recently uploaded (20)

PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Virus sequence retrieval from NCBI database
yamunaK13
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 

Closer look at classes

  • 1. Closer look at classes Overloading Methods Overloading Constructor Objects as parameter to methods Objects as parameter to constructor Returning Objects Recursion String Class String Buffer Class Command line arguments Access Controle Static Keyword Usage Final Keyword Usage
  • 2. Overloading methods • Overloading is ability of one function to perform different tasks. • it allows creating several methods with the same name which differ from each other in the type of the input and the output class Demo_class { /* having same method name*/ void demo() { System.out.println("hello world"); } void demo(int a) { System.out.println("the value of a::"+a); } public static void main(String[] y) { Demo_class d=new Demo_class(); d.demo(); d.demo(10); } }
  • 3. Overloading Constructor • In addition to overloading normal methods,you can also overload constructor methods class Demo_Constructor_overload { Demo_Constructor_overload() { System.out.println("hello default constructor"); } Demo_Constructor_overload(String s) { System.out.println("hello default constructor 1::"+s); } public static void main(String[] a1) { Demo_Constructor_overload d=new Demo_Constructor_overload(); Demo_Constructor_overload d1=new Demo_Constructor_overload ("yugandhar"); } }
  • 4. Uses of method Overloading • The main advantage of this is cleanliness of code. • the use of function overloading is to save the memory space, consistency and readabiliy.
  • 5. Objects as parameters to methods class Rectangle { int lenght,width; Rectangle(int lenght,int width) { this.lenght=lenght; this.width=width; } void print_values(Rectangle r) { System.out.println("the value of member length "+r.lenght+"n the value of member variable width is::"+this.width); } } public class Demo_object_paramter { public static void main(String[] y) { Rectangle r=new Rectangle(10,20); r.print_values(r); } }
  • 6. Objects as parameters to Constructor class Rectangle { int lenght,width; Rectangle(int lenght,int width) { this.lenght=lenght; this.width=width; } Rectangle(Rectangle r) { System.out.println("the member varialbe lenght::"+r.lenght+"n the member varialbe height::"+r.width); } } public class Demo_object_paramter { public static void main(String[] y) { Rectangle r=new Rectangle(30,20); Rectangle r1=new Rectangle(r); } }
  • 7. Returning objects • A method can return any type of data,including class types that you create class Rectangle { int length,width; Rectangle demo(int length,int width) { this.length=length; this.width=width; return(this); } } public class Demo_object_paramter { public static void main(String[] y) { Rectangle r=new Rectangle(); Rectangle r1=r.demo(30,40); System.out.println("the value of length is::"+r1.length+"n the value of width is::"+r1.width); } }
  • 8. Recursion • Recursion is a basic programming technique you can use in Java, in which a method calls itself to solve some problem. • A method that uses this technique is recursive. class Factorial { int fact(int n) { int result; if(n==1) return 1; result=fact(n-1)*n; return result; } } public class Recursion_1 { public static void main(String[] y) { Factorial f=new Factorial(); System.out.println("the factorial value is::"+f.fact(5)); } }
  • 9. String Class • The first thing to understand about strings is that every string you create is actually an object of type string . Every string constant is actually a string Object. • Eg System.out.println(“hello world”); • The secound thing to understand about strings is that objects of type String are immutable once string objects are created ,its content can not be altered. public class Private_AccessDemo { public static void main(String[] y) { String s=new String("yugandhar"); System.out.println("the s String is::"+s); System.out.println("the String length::"+s.length()); String s1=new String("programmer"); System.out.println("the String concatination "+s+s1); } }
  • 10. Declaring String Array public static void main(String[] y) { String[] s=new String[3];//string array with 3 Scanner sc=new Scanner(System.in);//taking input at run time from user for(int i=0;i<s.length;i++) s[i]=sc.nextLine(); for(int i=0;i<s.length;i++) System.out.println(" "+s[i]); }
  • 11. Some String Methods public class Private_AccessDemo { public static void main(String[] y) { String s="yugandhar"; System.out.println("the upper case::"+s.toUpperCase()); System.out.println("the upper case::"+s.toLowerCase()); System.out.println("the replace ::"+s.replace('a','r')); System.out.println("the charAt ::"+s.charAt(8)); } }
  • 12. String Buffer Class • String Buffer creates the string of flexible length that can be modified in terms content and length. We can insert characters and substrings in the middle of the string are append another string at the end. public class Private_AccessDemo { public static void main(String[] y) { StringBuffer s=new StringBuffer("yugandhar");//StringBuffer constructor System.out.println("the String Buffer class"); s.setCharAt(6,'x');//methods of String Buffer class System.out.println(s); System.out.println("the append string::"+s.append(" programmer")); s.setLength(40);// methods of String Buffer class System.out.println("the set length ::"+s.length()); } }
  • 13. Command line arguments • A command-line argument is the information that directly follows the program’s name on the command line when it is excuted . • All command line arguments are passed as strings public static void main(String[] y) { System.out.println("the commandline arguments"); for(int i=0;i<y.length;i++) System.out.println("the y["+i+"]::"+y[i]); } ####output#### D:softwaresjava_programs>java Private_AccessDemo h e l l o w the commandline arguments the y[0]::h the y[1]::e the y[2]::l the y[3]::l the y[4]::o the y[5]::w
  • 14. Access Control • encapsulation provides another important attribute: access control. • Java’s access specifiers are public, private, and protected • Public: A class, method, constructor, interface etc declared public can be accessed from any other class. Therefore fields, methods, blocks declared inside a public class can be accessed from any class • private: Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself. Private access modifier is the most restrictive access level. Class and interfaces cannot be private • protect: Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces. • Default: access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.A variable or method declared without any access control modifier is available to any other class in the same package.
  • 15. Public access specifier package demo_package; import java.util.*; public class AccessDemo { public void test() { System.out.println("Example of public access specifier"); } protected int x=200; } D:softwaresjava_programs>javac -d . AccessDemo.java import java.util.*; import demo_package.AccessDemo; public class Public_AccessDemo { public static void main(String[] y) { AccessDemo d=new AccessDemo(); d.test(); } } ####output#### D:softwaresjava_programs>javac Public_AccessDemo.java D:softwaresjava_programs>java Public_AccessDemo Example of public access specifier
  • 16. Private Access modifyer class demo { private int x=20; private void demo() { System.out.println("the value of x is::"+x); } void demo_1() { System.out.println("the demo_1 value of x is::"+x); } } public class Private_AccessDemo { public static void main(String[] y) { demo d=new demo(); System.out.println("the value of x is"+d.x); d.demo(); d.demo_1(); } } ####output#### D:softwaresjava_programs>javac Private_AccessDemo.java error: x has private access in demo System.out.println("the value of x is"+d.x); : error: demo() has private access in demo d.demo() making the d.demo_1() above two lines as comment then following out put will get D:softwaresjava_programs>java Private_AccessDemo the demo_1 value of x is::20
  • 17. Protected access specifyer import java.util.*; import demo_package.AccessDemo; class demo { protected int x=20; protected void demo() { System.out.println("the value of x is::"+x); } } public class Private_AccessDemo extends AccessDemo { public static void main(String[] y) { demo d=new demo(); d.x=30; System.out.println("the value of x is "+d.x); d.demo(); /*try with this code to know the difference with out extends AccessDemo AccessDemo a=new AccessDemo(); System.out.println(a.x); -*/ Private_AccessDemo a=new Private_AccessDemo(); System.out.println(a.x); } } D:softwaresjava_programs>java Private_AccessDemo the value of x is 30 the value of x is::30 200
  • 18. Static Variable declaration with Static • When a variable is declared with the keyword “static”, its called a “class variable”. • All instances share the same copy of the variable. • A class variable can be accessed directly with the class, without the need to create a instance. class static_ { static String y="i am static variable"; String y1="hello_world"; } public class Static_Demo { public static void main(String[] y) { System.out.println("accessing static variable::"+static_.y); static_ s=new static_(); s.y="hello"; s.y1="H"; //static_.y="done"; System.out.println("accessing static variable::"+static_.y); /* * here y shares the same copy */ static_ s1=new static_(); System.out.println("accessing static variable::"+s1.y+"n the value of y1::"+s1.y1); } }
  • 19. Method declaration with static • It is a method which belongs to the class and not to the object(instance) • A static method can access only static data. It can not access non-static data (instance variables) • A static method can call only other static methods and can not call a non- static method from it. • A static method can be accessed directly by the class name and doesn’t need any object • Syntax : <class-name>.<method-name> • A static method cannot refer to “this” or “super” keywords in anyway
  • 20. class static_ { int x=10; static void demo() { System.out.println("hellow static method"); // System.out.println("the value of x is"+x); /* * static methods can call only other static methods * and other static variables only */ //demo_1(); demo_2(); } void demo_1() { System.out.println("not static method demo_1()"); } static void demo_2() { System.out.println(" static method demo_2()"); } }//class public class Static_Demo { static void demo_3() { System.out.println("the main methods static"); } public static void main(String[] y) { System.out.println("static method"); static_.demo(); demo_3(); } } ####output#### static method hellow static method static method demo_2() the main methods static
  • 21. Static block • The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the JVM. class static_ { int x=10; void demo() { System.out.println("hellow"); } static{ System.out.println("secound static block"); } static{ System.out.println("first static block"); } } public class Static_Demo { public static void main(String[] y) { static_ s=new static_(); s.demo(); } } ####output#### secound static block first static block hellow
  • 22. final keyword • final is a reserved keyword in Java to restrict the user and it can be applied to member variables, methods, class and local variables. class Demo { final int INT_VARIABLE=120; /**final variable value can not be chage */ public static void main(String[] y) { Demo d=new Demo(); d.INT_VARIABLE=45; } } ####output#### C:UsersYugandharDesktopjsp>javac Demo.java Demo.java:8: error: cannot assign a value to final variable INT_VARIABLE d.INT_VARIABLE=45;
  • 23. Final method • A final method cannot be overridden. Which means even though a sub class can call the final method of parent class without any issues but it cannot override it. class Super { final void demo() { System.out.println("hello"); } } class Sub_ extends Super { /*void demo() { System.out.println("hello world"); }*/ void demo_1() { System.out.println("hello world"); } } class Demo { public static void main(String[] y) { Sub_ s=new Sub_(); s.demo(); } } try to remove those comments to understand ####output#### C:UsersYugandharDesktopjsp>javac Demo.java Demo.java:11: error: demo() in Sub_ cannot override demo() in Super C:UsersYugandharDesktopjsp>java Demo hello
  • 24. Final class • We cannot extend a final class prevent inheritance final class Super { void demo() { System.out.println("hello"); } } class Sub_ extends s { void demo_1() { System.out.println("hello world"); } } class Demo { public static void main(String[] y) { Sub_ s=new Sub_(); } } ####output#### C:UsersYugandharDesktopjsp>javac Demo.java Demo.java:9: error: cannot inherit from final Super class Sub_ extends Super
  • 25. Uses of final keyword • Using final to define constants • Using final to prevent inheritance • Using final to prevent overriding • Using final for method arguments