SlideShare a Scribd company logo
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 Programs
vvpadhu
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct class
vishal choudhary
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
Deepak Singh
 
Static binding
Static bindingStatic binding
Static binding
vishal choudhary
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
Intro C# Book
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Interface java
Interface java Interface java
Interface java
atiafyrose
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
OUM SAOKOSAL
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and Polymorphism
Eng Teong Cheah
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
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 2
HamesKellor
 
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
HoneyChintal
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
Intro C# Book
 
Interface in java
Interface in javaInterface in java
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
Abid Kohistani
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 

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

Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
Kapish Joshi
 
2 object orientation-copy
2 object orientation-copy2 object orientation-copy
2 object orientation-copyJoel Campos
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
Mahmoud Alfarra
 
Language fundamentals ocjp
Language fundamentals ocjpLanguage fundamentals ocjp
Language fundamentals ocjp
Bhavishya sharma
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
R.K.College of engg & Tech
 
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
Sushant Choudhary
 
Java concurrency questions and answers
Java concurrency questions and answers Java concurrency questions and answers
Java concurrency questions and answers
CodeOps Technologies LLP
 
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
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
MLG College of Learning, Inc
 
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. .pptx
Poonam60376
 
Core java
Core javaCore java
Core java
prabhatjon
 
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 7
IIUM
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
Gousalya Ramachandran
 
Java.lang.object
Java.lang.objectJava.lang.object
Java.lang.object
Soham Sengupta
 
OCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertionsOCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertions
Hari kiran G
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
Karthik Rohan
 

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

Python: The Versatile Programming Language - Introduction
Python: The Versatile Programming Language - IntroductionPython: The Versatile Programming Language - Introduction
Python: The Versatile Programming Language - Introduction
Jainul Musani
 
Python a Versatile Programming Language - Introduction
Python a Versatile Programming Language - IntroductionPython a Versatile Programming Language - Introduction
Python a Versatile Programming Language - Introduction
Jainul Musani
 
React js t8 - inlinecss
React js   t8 - inlinecssReact js   t8 - inlinecss
React js t8 - inlinecss
Jainul Musani
 
React js t7 - forms-events
React js   t7 - forms-eventsReact js   t7 - forms-events
React js t7 - forms-events
Jainul Musani
 
React js t6 -lifecycle
React js   t6 -lifecycleReact js   t6 -lifecycle
React js t6 -lifecycle
Jainul Musani
 
React js t5 - state
React js   t5 - stateReact js   t5 - state
React js t5 - state
Jainul Musani
 
React js t4 - components
React js   t4 - componentsReact js   t4 - components
React js t4 - components
Jainul Musani
 
React js t3 - es6
React js   t3 - es6React js   t3 - es6
React js t3 - es6
Jainul Musani
 
React js t2 - jsx
React js   t2 - jsxReact js   t2 - jsx
React js t2 - jsx
Jainul Musani
 
React js t1 - introduction
React js   t1 - introductionReact js   t1 - introduction
React js t1 - introduction
Jainul Musani
 
ExpressJs Session01
ExpressJs Session01ExpressJs Session01
ExpressJs Session01
Jainul Musani
 
NodeJs Session03
NodeJs Session03NodeJs Session03
NodeJs Session03
Jainul Musani
 
NodeJs Session02
NodeJs Session02NodeJs Session02
NodeJs Session02
Jainul Musani
 
Nodejs Session01
Nodejs Session01Nodejs Session01
Nodejs Session01
Jainul Musani
 
Java exercise1
Java exercise1Java exercise1
Java exercise1
Jainul Musani
 
Fundamentals of JDBC
Fundamentals of JDBCFundamentals of JDBC
Fundamentals of JDBC
Jainul Musani
 
Core Java Special
Core Java SpecialCore Java Special
Core Java Special
Jainul Musani
 
Cassandra-vs-MongoDB
Cassandra-vs-MongoDBCassandra-vs-MongoDB
Cassandra-vs-MongoDB
Jainul Musani
 
MongoDB-SESSION03
MongoDB-SESSION03MongoDB-SESSION03
MongoDB-SESSION03
Jainul Musani
 
MongoDB-SESSION02
MongoDB-SESSION02MongoDB-SESSION02
MongoDB-SESSION02
Jainul Musani
 

More from Jainul Musani (20)

Python: The Versatile Programming Language - Introduction
Python: The Versatile Programming Language - IntroductionPython: The Versatile Programming Language - Introduction
Python: The Versatile Programming Language - Introduction
 
Python a Versatile Programming Language - Introduction
Python a Versatile Programming Language - IntroductionPython a Versatile Programming Language - Introduction
Python a Versatile Programming Language - Introduction
 
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
 

Recently uploaded

The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 

Recently uploaded (20)

The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 

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(); } }