SlideShare a Scribd company logo
1 of 28
1
JAVA
Special Topics-1
Object Oriented
Technology
Jainul A. Musani
First Program
2
Jainul A. Musani
Class test1
{
public static void main(String s[])
{
System.out.println(“Good Morning…”);
}
}
Can we overload main method?
3
Jainul A. Musani
Normal Overloading
sum(int a, int b)
sum(double a, double b)
sum(int a, int b, int c)
Can we overload main method?
4
Jainul A. Musani
public class MyMainOverload
{
public static void main(Integer args)
{ S.o.p(“int Args in main() method ");
}
public static void main(char args)
{ S.o.p(“char Args main() method ");
}
//Original main() method
public static void main(String[] args)
{ S.o.p("Original main() method ");
}
}
Can we overload main method?
5
Jainul A. Musani
public class MyMainOverload
{
public static void main(Integer args)
{ S.o.p(“int Args in main() method ");
}
public static void main(char args)
{ S.o.p(“char Args main() method ");
}
//Original main() method
public static void main(String[] args)
{ S.o.p("Original main() method ");
}
new MyMainOverload();
new MyMainOverload.main(10);
new MyMainOverload.main(‘Z’);
}
Constructor returns a value but, what?
6
Jainul A. Musani
Constructor Types:
1. Default Constructor
2. Parameterized Constructor
Java constructor doesn't return any specific value,
which can directly affect the code.
A constructor is called by the memory allocation and
object initialization code at runtime.
The Constructor is more responsible for assigning
the initial values to the data members of the class.
A constructor returns the instance created by the new
keyword in our
Constructor returns a value but, what?
7
Jainul A. Musani
public class Emp {
int EmpNo;
String EmpName;
Emp(){
System.out.println("I am a Emp-Constructor");
}
public static void main(String[] args) {
Emp e1 = new Emp();
System. out. println(e1); // Emp@4dc63996
}
}
Singleton Class using private constructor
8
Jainul A. Musani
To create a class that can be instantiated
only once at a time.
Steps
1.Define the class constructor as private.
2.Create a static method that returns the object of
this class.
Singleton Class using private constructor
9
Jainul A. Musani
public class CAR{
private static CAR bmw = null;
public String msg;
private CAR() {}
public static CAR getAcar() {
If (bmw == null){
bmw = new CAR();
}
return bmw;
}
Singleton Class using private constructor
10
Jainul A. Musani
public static void main(String[] args) {
CAR mycar = CAR.getAcar();
mycar.msg = “My BMW is no.1";
CAR mycar2 = CAR.getAcar();
mycar2.msg = “My New BMW No.2";
CAR mycar3 = CAR.getAcar();
mycar3.msg = “My Vintage Car No. 3";
System.out.println(“Car 1
: "+mycar.msg);
System.out.println(“Car 2 : "+
mycar2.msg);
System.out.println(“Car 3 : "+
mycar3.msg);
Singleton Class using private constructor
11
Jainul A. Musani
OUTPUT:
Car 1 : My Vintage Car No. 3
Car 2 : My Vintage Car No. 3
Car 3 : My Vintage Car No. 3
Can constructor be private in Java?
12
Jainul A. Musani
Java allows to declare a constructor as
private.
Declare a constructor private by using
the private access specifier.
Note that if a constructor is declared
private, we are not able to create an
object of the class. Instead, we can use
this private constructor in Singleton
Design Pattern.
Can we create a program without main method?
13
Jainul A. Musani
Using STATIC Block:
class A{
static{
System.out.println("class without a main method");
System.exit(0);
}
}
This is valid till the Java 6 version. From Java 7 onwards the
compiler JVM will give you error message – main() method
missing.
Can main() method as Non-Static in java?
14
Jainul A. Musani
No.
Error will display
Multiple inheritance is not supported in java?
15
Jainul A. Musani
Ambiguity in the derived class.
ABC extends String – toString() method
override,
FLYER - makeFly() method
BIRD extends ABC, FLYER – doAnything()
method
ObjBird – can’t call the methods available in
ABC class (i.e. toString()) it will not able to
decide from which method it should invoke
from ABC or from FLYER?
Can We Override static method?
16
Jainul A. Musani
Overload – YES
Override – NO
BUT,
the method is written in derived class
Override – YES :
Can We Override static method?
17
Jainul A. Musani
Overload
public class Test {
public static void foo() {
System.out.println("Test.foo() called ");
}
public static void foo(int a) {
System.out.println("Test.foo(int) called ");
}
public static void main(String args[])
{
Test.foo();
Test.foo(10);
}
}
Can We Override static method?
18
Jainul A. Musani
Override
public class Test {
public static void foo() {
System.out.println("Test.foo() called ");
}
public static void foo() {// Compiler Error: cannot redefine foo()
System.out.println("Test.foo() called ");
}
public static void main(String args[])
{
Test.foo();
Test.foo(10);
}
}
Can We Override static method?
19
Jainul A. Musani
In Derived Class
class Base {
public static void display() {
System.out.println("Static or class
method from Base");
}
public void print() {
System.out.println("Non-static or Instance
method from Base");
}
}
Can We Override static method?
20
Jainul A. Musani
In Derived Class
class Derived extends Base {
public static void display() {
System.out.println("Static or class
method from Derived");
}
System.out.println("Non-static or Instance
method from Derived");
}
}
public class Test {
public static void main(String args[ ]) {
Base obj1 = new Derived();
obj1.display();
obj1.print();
}
}
Can We Override static method?
21
Jainul A. Musani
Static or class method from Base
Non-static or Instance method from
Derived
Output
In Derived Class
What is covariant return type?
22
Jainul A. Musani
class A1
{
A1 foo() {
return this;
}
void print(){
System.out.println(“Inside class A1”);
}
}
1
What is covariant return type?
23
Jainul A. Musani
class A2 extends A1
{
@Override
A2 foo() {
return this;
}
void print(){
System.out.println(“Inside class A2”);
}
}
2
What is covariant return type?
24
Jainul A. Musani
class A3 extends A2
{
@Override
A2 foo() {
return this;
}
@Override
void print(){
System.out.println(“Inside class A3”);
}
}
3
What is covariant return type?
25
Jainul A. Musani
public class CovariatEx
{
public static void main(String args[]) {
A1 obj1 = new A1();
obj1.foo().print()  ‘Inside class A1’
A2 obj2 = new A2();
obj2.foo().print()  ‘Inside class A2’
A3 obj3 = new A3();
obj3.foo().print()  ‘Inside class A3’
}
}
4
What is covariant return type?
26
Jainul A. Musani
public class CovariatEx
{
public static void main(String args[]) {
A1 obj1 = new A1();
obj1.foo().print()  ‘Inside class A1’
A2 obj2 = new A2();
((A2)obj2).foo().print()  ‘Inside class A2’
A3 obj3 = new A3();
((A3)obj3).foo().print()  ‘Inside class A3’
}
}
5
Why use instance initializer block?
27
Jainul A. Musani
 static block v/s initializer block
 initializer v/s constructor
 super() calls only constructor not
initializer block but when before
constructor initializer block always calls.
So indirectly super() calls initializer
block.
initializer block
28
Jainul A. Musani
public class Bike
{
int speed;
Bike(){ S.o.p(“Bike Speed is : “ + speed); }
{ // initializer block
speed = 100;
}
public static void main(String args[]) {
Bike b1 = new Bike();
Bike b2 = new Bike();
}
}

More Related Content

What's hot

Java Programs
Java ProgramsJava Programs
Java Programsvvpadhu
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct classvishal choudhary
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-javaDeepak Singh
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstractionIntro C# Book
 
Interface java
Interface java Interface java
Interface java atiafyrose
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 InterfaceOUM SAOKOSAL
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismEng Teong Cheah
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_typesAbed Bukhari
 
No more loops with lambdaj
No more loops with lambdajNo more loops with lambdaj
No more loops with lambdajMario Fusco
 
CMSC 350 HOMEWORK 2
CMSC 350 HOMEWORK 2CMSC 350 HOMEWORK 2
CMSC 350 HOMEWORK 2HamesKellor
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism Intro C# Book
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#Abid Kohistani
 

What's hot (20)

Java Programs
Java ProgramsJava Programs
Java Programs
 
C sharp part2
C sharp part2C sharp part2
C sharp part2
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct class
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
Static binding
Static bindingStatic binding
Static binding
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Interface java
Interface java Interface java
Interface java
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and Polymorphism
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
 
No more loops with lambdaj
No more loops with lambdajNo more loops with lambdaj
No more loops with lambdaj
 
CMSC 350 HOMEWORK 2
CMSC 350 HOMEWORK 2CMSC 350 HOMEWORK 2
CMSC 350 HOMEWORK 2
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
 
Java interface
Java interfaceJava interface
Java interface
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Java interface
Java interfaceJava interface
Java interface
 

Similar to Core Java Special

2 object orientation-copy
2 object orientation-copy2 object orientation-copy
2 object orientation-copyJoel Campos
 
Language fundamentals ocjp
Language fundamentals ocjpLanguage fundamentals ocjp
Language fundamentals ocjpBhavishya sharma
 
Indus Valley Partner aptitude questions and answers
Indus Valley Partner aptitude questions and answersIndus Valley Partner aptitude questions and answers
Indus Valley Partner aptitude questions and answersSushant Choudhary
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Palak Sanghani
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxPoonam60376
 
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...Udayan Khattry
 
Assignment 7
Assignment 7Assignment 7
Assignment 7IIUM
 
OCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertionsOCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertionsHari kiran G
 

Similar to Core Java Special (20)

Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
2 object orientation-copy
2 object orientation-copy2 object orientation-copy
2 object orientation-copy
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
Language fundamentals ocjp
Language fundamentals ocjpLanguage fundamentals ocjp
Language fundamentals ocjp
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 
Indus Valley Partner aptitude questions and answers
Indus Valley Partner aptitude questions and answersIndus Valley Partner aptitude questions and answers
Indus Valley Partner aptitude questions and answers
 
Java concurrency questions and answers
Java concurrency questions and answers Java concurrency questions and answers
Java concurrency questions and answers
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
 
7
77
7
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 
Core java
Core javaCore java
Core java
 
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
Java.lang.object
Java.lang.objectJava.lang.object
Java.lang.object
 
OCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertionsOCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertions
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 

More from Jainul Musani

More from Jainul Musani (20)

React js t8 - inlinecss
React js   t8 - inlinecssReact js   t8 - inlinecss
React js t8 - inlinecss
 
React js t7 - forms-events
React js   t7 - forms-eventsReact js   t7 - forms-events
React js t7 - forms-events
 
React js t6 -lifecycle
React js   t6 -lifecycleReact js   t6 -lifecycle
React js t6 -lifecycle
 
React js t5 - state
React js   t5 - stateReact js   t5 - state
React js t5 - state
 
React js t4 - components
React js   t4 - componentsReact js   t4 - components
React js t4 - components
 
React js t3 - es6
React js   t3 - es6React js   t3 - es6
React js t3 - es6
 
React js t2 - jsx
React js   t2 - jsxReact js   t2 - jsx
React js t2 - jsx
 
React js t1 - introduction
React js   t1 - introductionReact js   t1 - introduction
React js t1 - introduction
 
ExpressJs Session01
ExpressJs Session01ExpressJs Session01
ExpressJs Session01
 
NodeJs Session03
NodeJs Session03NodeJs Session03
NodeJs Session03
 
NodeJs Session02
NodeJs Session02NodeJs Session02
NodeJs Session02
 
Nodejs Session01
Nodejs Session01Nodejs Session01
Nodejs Session01
 
Java exercise1
Java exercise1Java exercise1
Java exercise1
 
Fundamentals of JDBC
Fundamentals of JDBCFundamentals of JDBC
Fundamentals of JDBC
 
Core Java Special
Core Java SpecialCore Java Special
Core Java Special
 
Cassandra-vs-MongoDB
Cassandra-vs-MongoDBCassandra-vs-MongoDB
Cassandra-vs-MongoDB
 
MongoDB-SESSION03
MongoDB-SESSION03MongoDB-SESSION03
MongoDB-SESSION03
 
MongoDB-SESSION02
MongoDB-SESSION02MongoDB-SESSION02
MongoDB-SESSION02
 
MongoDB-SESION01
MongoDB-SESION01MongoDB-SESION01
MongoDB-SESION01
 
4+1archi
4+1archi4+1archi
4+1archi
 

Recently uploaded

Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

Core Java Special

  • 2. First Program 2 Jainul A. Musani Class test1 { public static void main(String s[]) { System.out.println(“Good Morning…”); } }
  • 3. Can we overload main method? 3 Jainul A. Musani Normal Overloading sum(int a, int b) sum(double a, double b) sum(int a, int b, int c)
  • 4. Can we overload main method? 4 Jainul A. Musani public class MyMainOverload { public static void main(Integer args) { S.o.p(“int Args in main() method "); } public static void main(char args) { S.o.p(“char Args main() method "); } //Original main() method public static void main(String[] args) { S.o.p("Original main() method "); } }
  • 5. Can we overload main method? 5 Jainul A. Musani public class MyMainOverload { public static void main(Integer args) { S.o.p(“int Args in main() method "); } public static void main(char args) { S.o.p(“char Args main() method "); } //Original main() method public static void main(String[] args) { S.o.p("Original main() method "); } new MyMainOverload(); new MyMainOverload.main(10); new MyMainOverload.main(‘Z’); }
  • 6. Constructor returns a value but, what? 6 Jainul A. Musani Constructor Types: 1. Default Constructor 2. Parameterized Constructor Java constructor doesn't return any specific value, which can directly affect the code. A constructor is called by the memory allocation and object initialization code at runtime. The Constructor is more responsible for assigning the initial values to the data members of the class. A constructor returns the instance created by the new keyword in our
  • 7. Constructor returns a value but, what? 7 Jainul A. Musani public class Emp { int EmpNo; String EmpName; Emp(){ System.out.println("I am a Emp-Constructor"); } public static void main(String[] args) { Emp e1 = new Emp(); System. out. println(e1); // Emp@4dc63996 } }
  • 8. Singleton Class using private constructor 8 Jainul A. Musani To create a class that can be instantiated only once at a time. Steps 1.Define the class constructor as private. 2.Create a static method that returns the object of this class.
  • 9. Singleton Class using private constructor 9 Jainul A. Musani public class CAR{ private static CAR bmw = null; public String msg; private CAR() {} public static CAR getAcar() { If (bmw == null){ bmw = new CAR(); } return bmw; }
  • 10. Singleton Class using private constructor 10 Jainul A. Musani public static void main(String[] args) { CAR mycar = CAR.getAcar(); mycar.msg = “My BMW is no.1"; CAR mycar2 = CAR.getAcar(); mycar2.msg = “My New BMW No.2"; CAR mycar3 = CAR.getAcar(); mycar3.msg = “My Vintage Car No. 3"; System.out.println(“Car 1 : "+mycar.msg); System.out.println(“Car 2 : "+ mycar2.msg); System.out.println(“Car 3 : "+ mycar3.msg);
  • 11. Singleton Class using private constructor 11 Jainul A. Musani OUTPUT: Car 1 : My Vintage Car No. 3 Car 2 : My Vintage Car No. 3 Car 3 : My Vintage Car No. 3
  • 12. Can constructor be private in Java? 12 Jainul A. Musani Java allows to declare a constructor as private. Declare a constructor private by using the private access specifier. Note that if a constructor is declared private, we are not able to create an object of the class. Instead, we can use this private constructor in Singleton Design Pattern.
  • 13. Can we create a program without main method? 13 Jainul A. Musani Using STATIC Block: class A{ static{ System.out.println("class without a main method"); System.exit(0); } } This is valid till the Java 6 version. From Java 7 onwards the compiler JVM will give you error message – main() method missing.
  • 14. Can main() method as Non-Static in java? 14 Jainul A. Musani No. Error will display
  • 15. Multiple inheritance is not supported in java? 15 Jainul A. Musani Ambiguity in the derived class. ABC extends String – toString() method override, FLYER - makeFly() method BIRD extends ABC, FLYER – doAnything() method ObjBird – can’t call the methods available in ABC class (i.e. toString()) it will not able to decide from which method it should invoke from ABC or from FLYER?
  • 16. Can We Override static method? 16 Jainul A. Musani Overload – YES Override – NO BUT, the method is written in derived class Override – YES :
  • 17. Can We Override static method? 17 Jainul A. Musani Overload public class Test { public static void foo() { System.out.println("Test.foo() called "); } public static void foo(int a) { System.out.println("Test.foo(int) called "); } public static void main(String args[]) { Test.foo(); Test.foo(10); } }
  • 18. Can We Override static method? 18 Jainul A. Musani Override public class Test { public static void foo() { System.out.println("Test.foo() called "); } public static void foo() {// Compiler Error: cannot redefine foo() System.out.println("Test.foo() called "); } public static void main(String args[]) { Test.foo(); Test.foo(10); } }
  • 19. Can We Override static method? 19 Jainul A. Musani In Derived Class class Base { public static void display() { System.out.println("Static or class method from Base"); } public void print() { System.out.println("Non-static or Instance method from Base"); } }
  • 20. Can We Override static method? 20 Jainul A. Musani In Derived Class class Derived extends Base { public static void display() { System.out.println("Static or class method from Derived"); } System.out.println("Non-static or Instance method from Derived"); } }
  • 21. public class Test { public static void main(String args[ ]) { Base obj1 = new Derived(); obj1.display(); obj1.print(); } } Can We Override static method? 21 Jainul A. Musani Static or class method from Base Non-static or Instance method from Derived Output In Derived Class
  • 22. What is covariant return type? 22 Jainul A. Musani class A1 { A1 foo() { return this; } void print(){ System.out.println(“Inside class A1”); } } 1
  • 23. What is covariant return type? 23 Jainul A. Musani class A2 extends A1 { @Override A2 foo() { return this; } void print(){ System.out.println(“Inside class A2”); } } 2
  • 24. What is covariant return type? 24 Jainul A. Musani class A3 extends A2 { @Override A2 foo() { return this; } @Override void print(){ System.out.println(“Inside class A3”); } } 3
  • 25. What is covariant return type? 25 Jainul A. Musani public class CovariatEx { public static void main(String args[]) { A1 obj1 = new A1(); obj1.foo().print()  ‘Inside class A1’ A2 obj2 = new A2(); obj2.foo().print()  ‘Inside class A2’ A3 obj3 = new A3(); obj3.foo().print()  ‘Inside class A3’ } } 4
  • 26. What is covariant return type? 26 Jainul A. Musani public class CovariatEx { public static void main(String args[]) { A1 obj1 = new A1(); obj1.foo().print()  ‘Inside class A1’ A2 obj2 = new A2(); ((A2)obj2).foo().print()  ‘Inside class A2’ A3 obj3 = new A3(); ((A3)obj3).foo().print()  ‘Inside class A3’ } } 5
  • 27. Why use instance initializer block? 27 Jainul A. Musani  static block v/s initializer block  initializer v/s constructor  super() calls only constructor not initializer block but when before constructor initializer block always calls. So indirectly super() calls initializer block.
  • 28. initializer block 28 Jainul A. Musani public class Bike { int speed; Bike(){ S.o.p(“Bike Speed is : “ + speed); } { // initializer block speed = 100; } public static void main(String args[]) { Bike b1 = new Bike(); Bike b2 = new Bike(); } }