Mahika Tutorials sharing PPT slide for core java programming language. Go threw this slide and visit our YouTube page too
https://www.youtube.com/c/mahikatutorials
3. Java
ī Java was created by James Gosling at Sun Microsystems (which is now acquired by
Oracle Corporation) and released in 1995.
ī Java is a high level, object-oriented programming language.
7. Inheritance
īA mechanism by which a class acquires all the properties and behaviours of an
existing class.
īProvides code reusability.
īUsed to achieve runtime polymorphism.
8. Polymorphism
īAbility to take multiple forms.
Example-
int a=10,b=20,c;
c=a+b; //addition
String firstname=âSachinâ, lastname=âTendulkarâ, fullname;
fullname=firstname+lastname;//concatenation
9. OOPLs
(Four OOPs features)
Partial OOPL Pure OOPLFully OOPL
īClasses not mandatory.
īData members and methods can be
given outside class.
īe.g. C++
īClasses mandatory.
īData members and methods cannot
be given outside class.
īe.g. Java
īClasses mandatory.
īData members and methods cannot
be given outside class.
īPrimitive data types not supported.
īe.g. Smalltalk
Java supports primitive data types , hence it is a fully OOPL but not pure OOPL.
int i=20; //primitive type
Integer a=new Integer(20); //Class type
14. public static void main(String[] args) {
âĻâĻâĻâĻ..
âĻâĻâĻâĻ..
}
15. class Test
{
public static void myStatic()
{
-----
-----
-----
}
public void myNonStatic()
{
-----
}
}
class Sample
{
public void CallerFunction()
{
// Invoking static function
Test.myStatic();
// Invoking non-static function
Test t= new Test();
t.myNonStatic();
}
}
Invoking Member Funtions Of A Class
16. class Test
{
public static void main(String[] args)
{
-----
-----
-----
}
}
// Incase of static main()
Test.main();
Invoking main()
īSince the main method is static Java virtual Machine can call it without creating any
instance of a class which contains the main method.
// Incase of non-static main()
Test t=new Test()
t.main();
19. JVM
īJVM (Java Virtual Machine) is an abstract machine. It is a specification
that provides runtime environment in which java bytecode can be
executed.
īJVM is the engine that drives the java code
īThe JVM performs following main tasks:
ī§ Loads code
ī§ Verifies code
ī§ Executes code
20. JRE
ī JRE is an acronym for Java Runtime Environment.
JRE
JVM
Llibraries
like rt.jar
Other files
21. JRE
JVM
Llibraries
like rt.jar
Other files
Development
tools like
javac, java etc.
JDK
JDK
īJDK is an acronym for Java Development Kit.
īIncludes a complete JRE (Java Runtime Environment) plus tools for developing, debugging, and
monitoring Java applications.
22. Internal Architecture of JVM
Java
Runtime
System
Class Loader
Class
Area
Heap Stack
PC
Register
Native
Method
Stack
Execution
engine
Native Method
Interface
Java Native
Libraries
Memory areas
allocated by JVM
23. Internal Architecture of JVM
Java
Runtime
System
Class Loader
Class
Area
Heap Stack
PC
Register
Native
Method
Stack
Execution
engine
Native Method
Interface
Java Native
Libraries
Memory areas
allocated by JVM
24. Java Keywords
īWords that cannot be used as identifiers in programs.
īThere are 50 java keywords.
īAll keywords are in lower-case.
īEach keyword has special meaning for the compiler.
īKeywords that are not used in Java so far are called reserved words.
25. Category Keywords
Access modifiers private, protected, public
Class, method, variable
modifiers
abstract, class, extends, final, implements, interface, native,new, static,
strictfp, synchronized, transient, volatile
Flow control break, case, continue, default, do, else, for, if, instanceof,return, switch,
while
Package control import, package
Primitive types boolean, byte, char, double, float, int, long, short
Exception handling assert, catch, finally, throw, throws, try
Enumeration enum
Others super, this, void
Unused const, goto
Points to remember
ī const and goto are resevered words.
ī true, false and null are literals, not keywords.
List of Java Keywords
26. Rules for Naming Java Identifiers
īAllowed characters for identifiers are[A-Z], [a-z],[0-9], â$â(dollar sign) and â_â
(underscore).
īShould not start with digits(0-9).
īCase-sensitive, like id and ID are different.
īJava keyword cannot be used as an identifier.
Java Identifiers
īNames given to programming elements like variables, methods,
classes, packages and interfaces.
27. Java Naming conventions
Name Convention
class name should start with uppercase letter and be a noun e.g.Shape, String, Color, System, Thread etc.
interface name should start with uppercase letter and be an adjective e.g. Runnable, Clonable etc.
method name should start with lowercase letter and be a verb e.g. compute(), print(), println() etc.
variable name should start with lowercase letter e.g. firstName, lastName etc.
package name should be in lowercase letter e.g. java, lang, sql, util etc.
constants name should be in uppercase letter. e.g. BLUE, MAX_PRIORITY etc.
29. Class
ī A template that describes the data and behavior.
Object
īAn entity that has state and behavior is known as an object e.g. chair, bike, table, car etc.
īŧstate: represents data (value) of an object.
īŧbehavior: represents the functionality of an object such as deposit, withdraw etc.
31. public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Java class example
33. Scanner class
īA class in java.util package
īUsed for obtaining the input of the primitive types like int, double etc. and strings
īBreaks its input into tokens using a delimiter pattern, which by default matches whitespace.
īTo create an object of Scanner class, we usually pass the predefined object System.in, which
represents the standard input stream.
īWe may pass an object of class File if we want to read input from a file.
34. Creating instance of Scanner class
To read from System.in:
Scanner sc = new Scanner(System.in);
To read from File:
Scanner sc = new Scanner(new File("myFile"));
35. Commonly used methods of Scanner class
Method Description
public String next() it returns the next token from the scanner.
public byte nextByte() it scans the next token as a byte.
public short nextShort() it scans the next token as a short value.
public int nextInt() it scans the next token as an int value.
public long nextLong() it scans the next token as a long value.
public float nextFloat() it scans the next token as a float value.
public double nextDouble() it scans the next token as a double value.
42. Data Type Default Value Default size
boolean false 1 bit
char 'u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
lowest unicode value:u0000
highest unicode value:uFFFF
43. Control Structures
1. Conditional
a) Simple if
b) ifâelse
c) if-else-if ladder
d) Nested if
e) switch case
2. Looping
a) while loop
b) for loop
c) do while loop
d) for each/enhanced for loop
46. if-else-if ladder Statement
if(condition1){
//code to be executed if condition1 is true
}
else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Test
Expression 1
Test
Expression 2
Test
Expression 3
No
No
No
Statement 1
Statement 2
Statement 3
else Body
Yes
Yes
Yes
47. if(Boolean_expression 1)
{
// Executes when the Boolean expression 1 is true
if(Boolean_expression 2)
{
// Executes when the Boolean expression 2 is true
}
}
Nested if
49. switch Statement
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
//code to be executed if all cases are not matched;
}
Expression
case 1?
case 2?
case 3?
No
No
No
Case Block 1
Case Block 2
Case Block 3
default Block
Yes
Yes
Yes
50. switch Statement
īExpression can be byte, short, char, and int.
īFrom JDK7 onwards, it also works with enumerated types ( Enums in java), the String class
and Wrapper classes.
īDuplicate case values are not allowed.
switch(ch)
{
case 1: .......
break;
case 1: ......
break;
âĻâĻâĻâĻ
}
51. īThe value for a case must be the same data type as the variable in the
switch.
switch(ch)
{
case 1: ......
break;
case 2: ......
break;
}
īThe value for a case must be a constant or a literal.Variables are not
allowed.
52. īThe break statement is used inside the switch to terminate a statement sequence.
īThe break statement is optional. If omitted, execution will continue on into the next
case.
īThe default statement is optional.
59. Method Overloading
īWhen a class have multiple methods by same name but different parameters.
Advantage :
īIncreases the readability of the program.
Different ways to overload the method:
1) By changing number of arguments
2) By changing the data type of arguments
Method Overloading is not possible by changing the return type or access specifier of a method.
61. ī If we have overloaded methods with one byte/short/int/long parameter then
always method with int is called.
ī To invoke method with long parameter L should be append to the value while
calling
ī Method with byte/short is called only if we pass the variable of that type or we
typecast the value.
ī If we have overloaded method with one int/float/double and we pass long value
then method with int parameter is called.
ī If we have overloaded method with one float/double and we pass long value
then method with float parameter is called.
62. Ambiguity in Method Overloading
class Test{
void sum(int a,long b){System.out.println(a+b);}
void sum(long a,int b){System.out.println(a+b);}
public static void main(String args[]){
Test obj=new Test();
obj.sum(20,20);// ambiguity
}
}
Output:Compile Time Error
63. Constructor
ī Special type of method that is used to initialize the object.
ī Invoked at the time of object creation.
ī Can be overloaded.
Rules for creating java constructor:
ī Constructor name must be same as its class name
ī Constructor must have no explicit return type
Types of java constructors:
ī Default constructor (no-arg constructor)
ī Parameterized constructor
64. īIf there is no constructor in a class, compiler automatically creates a
default constructor.
class Student{
}
class Student{
Student()
{
}
}
compiler
Student.java Student.class
65. Java Copy Constructor
There is no copy constructor in java. But, we can copy the values of one
object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in java.
They are:
ī By constructor
ī By assigning the values of one object into another
ī By clone() method of Object class
66. static keyword
īstatic is a non-access modifier.
īUsed for memory management mainly.
ī The static can be:
īąvariable (also known as class variable)
īąmethod (also known as class method)
īąblock
īąnested class
67. static variable
īUsed to refer the common property of all objects e.g. company name
of employees,college name of students etc.
īGets memory only once in class area at the time of class loading.
īLocal variables cannot be static.
Advantage :
īIt makes your program memory efficient (i.e it saves memory).
68. //Program for static variable
class Student{
int id;
String name;
static String college =âSVC";
Student(int i,String n){
id=i;
name = n;
}
void display (){
System.out.println(id+" "+name+" "+college);}
public static void main(String args[]){
Student s1 = new Student(1,âHriday");
Student s2 = new Student(2,âAnil");
s1.display();
s2.display();
}
}
id=1
name= âHridayâ
Stack
Heap
id=2
name= âAnilâ
s1
s2
college=âSVCââ
Class Area
69. static method
īMethod defined with the static keyword.
ī Belongs to the class rather than object of a class.
ī Can be invoked without the need for creating an instance of a class.
ī Can access static data members and can change the value of it.
ī Cannot use non static data member or call non-static method directly.
ī this and super cannot be used in static context.
70. static block
īUsed to initialize the static data member.
ī Is executed before main method at the time of classloading.
Example :
class A{
static{
System.out.println("static block invoked");
}
public static void main(String args[]){
System.out.println(âmain invoked");
}
}
71. this keyword in java
īA reference variable that refers to the current object.
Usage of this keyword :
īthis keyword can be used to refer current class instance variable.
īthis() can be used to invoke current class constructor.
īthis can be used to invoke current class method (implicitly)
īthis can be used to return the current class instance from the method.
īthis can be passed as an argument in the method call.
īthis can be passed as argument in the constructor call.
72. this() : to invoke current class constructor
īUsed for constructor chaining.
īUsed to reuse the constructor.
īCall to this() must be the first statement in constructor.
Syntax
this(); // call default constructor
this(value1,value2,.....) //call parametrized constructor
73. //this() : to invoke current class constructor
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 0, 0);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
âĻâĻâĻ.
}
74. this :to invoke current class method (implicitly)
īCan be used to invoke method of the current class.
īIf you don't use the this keyword, compiler automatically adds this
keyword while invoking the method.
class Test{
void m1( )
{ âĻ. }
void m2( )
{
m1()
}
âĻ.
}
class Test{
void m1( )
{ âĻ. }
void m2( )
{
this.m1()
}
âĻ.
}
compiler
Test.java Test.class
75. this: to pass as an argument in the method
class Test
{
int a;
int b;
Test()
{
a = 40;
b = 60;
}
// Method that receives 'this' keyword as parameter
void display(Test obj)
{
System.out.println("a = " + a + " b = " + b);
}
// Method that pases current class instance
void get()
{
display(this);
}
public static void main(String[] args)
{
Test object = new Test();
object.get();
}
}
Output:-
a=40 b=60
77. mahika.tutorials@gmail.com
this: to pass as argument in the constructor call
class B{
A obj;
B(A obj)
{
this.obj=obj;
}
void display(){
System.out.println(obj.data); //10
}
}
class A{
int data=10;
A()
{
B b=new B(this);
b.display();
}
public static void main(String args[]){
A a=new A();
}
}
78. this keyword in java
īA reference variable that refers to the current object.
Usage of this keyword :
ī this keyword can be used to refer current class instance variable.
ī this() can be used to invoke current class constructor.
ī this can be used to invoke current class method (implicitly)
ī this can be used to return the current class instance from the method.
ī this can be passed as an argument in the method call.
ī this can be passed as argument in the constructor call.
īļthis keyword cannot be used in static context.
79. Inheritance
ī A mechanism in which one object acquires all the properties and behaviors of parent object.
ī Represents the IS-A relationship, also known as parent-child relationship
ī extends keyword indicates that you are making a new class that derives from an existing class.
ī The meaning of "extends" is to increase the functionality.
ī A class which is inherited is called parent or super class and the new class is called child or
subclass.
Inheritance in java is used:
ī For Method Overriding (so runtime polymorphism can be achieved).
ī For Code Reusability.
Syntax of Java Inheritance:
class Subclass-name extends Superclass-name
{
//methods and fields
}
80. Types of inheritance in java
Multiple inheritance is not supported in java through class
81. īObject class is the superclass of all the classes in java by default.
īEvery class in Java is directly or indirectly derived from the Object class.
īObject class is present in java.lang package.
class A
{
âĻâĻ..
}
class A extends Object
{
âĻâĻ..
}
Object class
82. class A
{
âĻâĻ.
}
class B extends A
{
âĻâĻ.
}
class A extends Object
{
âĻâĻ..
}
class B extends A
{
âĻâĻ.
}
Object class
83. Commonly used Methods of Object class:
Method Description
public boolean equals(Object obj) compares the given object to this object.
protected Object clone() throws
CloneNotSupportedException
creates and returns the exact copy (clone) of this object.
public String toString() returns the string representation of this object.
public final void notify() wakes up single thread, waiting on this object's monitor.
public final void notifyAll() wakes up all the threads, waiting on this object's
monitor.
public final void wait(long timeout)throws
InterruptedException
causes the current thread to wait for the specified
milliseconds, until another thread notifies (invokes
notify() or notifyAll() method).
protected void finalize()throws Throwable is invoked by the garbage collector before object is being
garbage collected.
87. mahika.a.motwani@gmail.com
Multiple Inheritance
īA feature where a class can inherit properties of more than one parent class.
īJava doesnât allow multiple inheritance by using classes ,for simplicity and to avoid the ambiguity caused by it.
īIt creates problem during various operations like casting, constructor chaining etc.
88. mahika.a.motwani@gmail.com
Multiple Inheritance and Ambiguity
class A
{
void show()
{
System.out.println("Hello");}
}
class B
{
void show()
{
System.out.println("Welcome");
}
}
class C extends A,B //suppose if it was allowed
{
public static void main(String args[])
{
C obj=new C();
obj.show(); //ambiguity
}
}
92. Method Overriding
īIf subclass (child class) has the same method as declared in the parent class, it is known as method overriding
in java.
Usage :
ī Used to provide specific implementation of a method that is already provided by its super class.
ī Used for runtime polymorphism
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike extends Vehicle{
void run(){System.out.println("Bike is running â);}
public static void main(String args[]){
Bike obj = new Bike();
obj.run();
}
Example:
93. class Shape{
void draw()
{
System.out.println("No Shape");
}
}
class Rectangle extends Shape{
void draw()
{
System.out.println("Drawing rectangle");
}
}
class Circle extends Shape{
void draw()
{
System.out.println("Drawing circle");
}
}
class Triangle extends Shape{
void draw()
{
System.out.println("Drawing circle");
}
}
public class Test{
public static void main(String args[]){
Shape s;
s=new Shape();
s.draw();
s =new Circle();
s.draw();
s=new Rectangle();
s.draw();
s=new Triangle();
s.draw();
}
}
At compile-time:
94. class Shape{
void draw()
{
System.out.println("No Shape");
}
}
class Circle extends Shape{
void draw()
{
System.out.println("Drawing circle");
}
}
class Rectangle extends Shape{
void draw()
{
System.out.println("Drawing rectangle");
}
} class Triangle extends Shape{
void draw()
{
System.out.println("Drawing circle");
}
}
public class Test{
public static void main(String args[]){
Shape s;
s=new Shape();
s.draw();
s =new Circle();
s.draw();
s=new Rectangle();
s.draw();
s=new Triangle();
s.draw();
}
}
At run-time:
95. Rules for Method Overriding:
ī Method must have same name as in the parent class
ī Method must have same parameter as in the parent class.
ī Method must have same return type(or sub-type).
ī Must be IS-A relationship (inheritance).
ī private method cannot be overridden.
ī final method cannot be overridden
ī static method cannot be overridden because static method is bound with class whereas instance method
is bound with object.
ī We can not override constructor as parent and child class can never have constructor with same name.
ī Access Modifier of the overriding method (method of subclass) cannot be more restrictive than the
overridden method of parent class.
ī Binding of overridden methods happen at runtime which is known as dynamic binding.
96. ExceptionHandling with MethodOverriding in Java
ī If the superclass method does not declare an exception, subclass overridden
method cannot declare the checked exception.
class Child extends Parent{
void msg()throws IOException
{
System.out.println(âchild");
}
public static void main(String args[])
{
Parent p=new Child();
p.msg();
}
}
class Parent{
void msg()
{
System.out.println("parent");
}
}
Output:Compile Time Error
97. ī If the superclass method does not declare an exception, subclass
overridden method cannot declare the checked exception but can declare
unchecked exception.
class Parent{
void msg()
{
System.out.println("parent");
}
}
class Child extends Parent{
void msg()throws ArithmeticException
{
System.out.println("child");
}
public static void main(String args[])
{
Parent p=new Child();
p.msg();
}
}
Output:child
98. ī If the superclass method declares an exception, subclass overridden method can declare
same, subclass exception or no exception but cannot declare parent exception.
class Child extends Parent{
void msg()throws Exception
{
System.out.println("child");
}
public static void main(String args[])
{
Parent p=new Child();
try{
p.msg();
}catch(Exception e){
}
}
}
class Parent{
void msg()throws ArithmeticException{
System.out.println("parent");
}
}
Output:Compile Time Error
99. class Parent{
void msg()throws Exception{
System.out.println("parent");
}
}
Class Child extends Parent{
void msg()throws ArithmeticException{
System.out.println("child");
}
public static void main(String args[]){
Parent p=new Child();
try{
p.msg();
}catch(Exception e){}
}
}
Example in case subclass overridden method declares subclass exception
Output:child
100. Covariant Return Type
īSpecifies that the return type may vary in the same direction as the subclass.
īSince Java5, it is possible to override method by changing the return type if subclass overrides
any method, but it changes its return type to subclass type.
class ShapeFactory {
public Shape newShape()
{
âĻâĻâĻâĻ
}
}
class CircleFactory extends ShapeFactory {
public Circle newShape()
{
âĻâĻ
}
}
class Shape
{
âĻâĻâĻ
}
class Circle extends Shape
{
âĻâĻâĻ
}
101. super keyword
īA reference variable that is used to refer immediate parent class object.
īsuper keyword cannot be used in static context.
Usage :
ī super is used to refer immediate parent class instance variable.
ī super() is used to invoke immediate parent class constructor,.
ī super is used to invoke immediate parent class method.
102. super used to refer immediate parent class instance variable:
class Vehicle{
int speed=70;
}
class Bike extends Vehicle{
int speed=100;
void display(){
System.out.println(super.speed);// Vehicle speed
System.out.println(speed);// Bike speed
}
public static void main(String args[]){
Bike b=new Bike();
b.display();
}
}
103. super() to invoke parent class constructor:
class Vehicle{
Vehicle(){System.out.println("Vehicle created");}
}
class Bike extends Vehicle{
Bike(){
super();//invokes parent class constructor
System.out.println("Bike created");
}
public static void main(String args[]){
Bike b=new Bike();
}
}
104. ī If used, super() must be the first statement inside the constructor, hence either
super() or this() can be used inside the constructor.
ī super() is added in each class constructor automatically by compiler if there is
no super() or this().
class Bike{
Bike()
{
}
}
class Bike{
Bike()
{
super();
}
}
compiler
Bike.java Bike .classa
super() :to invoke parent class constructor
105. super used to invoke parent class method:
class Vehicle{
void show(){System.out.println("Vehicle Runing");}
}
class Bike extends Vehicle{
void show(){System.out.println("Bike Runing ");
}
void display(){
super.show();
show();
}
public static void main(String args[]){
Bike b=new Bike();
b.display();
}
}
106. final Keyword
īUsed to restrict the user.
īfinal can be:
1) Variableī If you make any variable as final, you cannot change the
value of final variable(It will be constant).
2) Method->If you make any methode as final, you cannot override it.
3) Class->If you make any class as final, you cannot extend it.
107. ī final method is inherited but you cannot override it.
ī A final variable that is not initialized at the time of declaration is known as blank final variable.
ī We can initialize blank final variable only in constructor. For example:
class Test{
final int i;//blank final variable
Test(){
i=80;
......
}
}
ī If a method is declared private and final in superclass then we can have method with the same signature
in subclass also because private is not inherited.
ī Constructor cannot be declared as final because constructor is never inherited.
final Keyword
109. Java Package
īGroup of similar types of classes, interfaces and sub-packages.
Types:
ībuilt-in package (such as java, lang, io, util, sql etc.)
īuser-defined package.
Advantages:
ī Used to categorize the classes and interfaces so that they can be easily maintained.
ī Provides access protection.
ī Removes naming collision.
110.
111. Ways to access the package from outside the package:
1) import packageName.*;
2) import packageName.classname;
3) fully qualified name.
īIf you import a package, subpackages will not be imported.
113. Access Modifiers
īSpecifies accessibility (scope) of a data member, method, constructor or class.
Types :
ī private
ī package-private (no explicit modifier)
ī protected
ī public
There are many non-access modifiers such as static, abstract, synchronized,
native, volatile, transient etc.
114. Access Modifier within class within package outside package by
subclass only
outside
package
private Y N N N
package-private (no explicit modifier) Y Y N N
protected Y Y Y N
public Y Y Y Y
Note: A class cannot be private or protected except nested class.
115. Mahika.a.motwani@gmail.com
Modifier A B C D
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
Visibiity
The following table shows where the members of the A class are visible for each of the access modifiers that can be
applied to them.
Package 1
A
B
Package 2
C
D
subclass
116. Java Array
īCollection of similar type of elements.
īContiguous memory location.
īFixed size.
īIndex based, first element of the array is stored at 0 index.
117. Types of Array:
īSingle Dimensional Array
īMultidimensional Array
Single Dimensional Array in java
Syntax:
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];
Instantiation :
arrayRefVar=new datatype[size];
Declaration, Instantiation and Initialization :
int a[]={33,3,4,5};//declaration, instantiation and initialization
119. Mahika.a.motwani@gmail.com
Diamond problem
īAn ambiguity that can arise as a consequence of allowing multiple inheritance.
īSince Java 8 default methods have been permitted inside interfaces, which may
cause ambiguity in some cases.
120. Mahika.a.motwani@gmail.com
Diamond Structure:Case#1
[+] void m () <<default>>
<<interface>>
A
<<interface>>
B
<<interface>>
C
[+] void main(String arg[]) <<static>>
<<class>>
D
interface A
{
default void m()
{
System.out.println("m() from interface A ");
}
}
interface B extends A
{
}
interface C extends A
{
}
public class D implements B,C {
public static void main(String[] args) {
new D().m();
}
}
121. Mahika.a.motwani@gmail.com
Diamond Structure:Case#2
[+] void m () <<default>>
<<interface>>
A
[+] void m () <<default>> <<override>>
<<interface>>
B
<<interface>>
C
[+] void main(String arg[]) <<static>>
<<class>>
D
interface A
{
default void m()
{
System.out.println("m() from interface A ");
}
}
interface B extends A
{
@Override
default void m()
{
System.out.println("m() from interface B ");
}
}
interface C extends A
{
}
public class D implements B,C {
public static void main(String[] args) {
new D().m();
}
}
122. Mahika.a.motwani@gmail.com
Diamond Structure:Case#3
[+] void m () <<default>>
<<interface>>
A
[+] void m () <<default>> <<override>>
<<interface>>
B
[+] void m () <<default>> <<override>>
<<interface>>
C
[+] void m () <<override>>
[+] void main(String arg[]) <<static>>
<<class>>
D
interface A
{
default void m()
{
System.out.println("m() from interface A ");
}
}
interface B extends A
{
@Override
default void m()
{
System.out.println("m() from interface B ");
}
}
interface C extends A
{
@Override
default void m()
{
System.out.println("m() from interface C");
}
}
public class D implements B,C {
@Override
public void m() {
B.super.m();
}
public static void main(String[] args) {
new D().m();
}
}