EvoNext
LESSON 12: POLYMORPHISM The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Types of polymorphism In Java , polymorphism is mainly dividedβ¦
LESSON 13: INHERITANCE
βοΈInheritance is one of the basic concept in object
orientation in which a new class is created by
absorbing an existing classβs members and
embellishing them with new or modified capabilities.
β The existing class is called the superclass, and the
new class is the subclass.
Syntax for inheritance
class SubClassName extends SuperClassName{};
Why And When To Use "Inheritance"?
- It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.
Let's explore by using sample code:-
βοΈInheritance is one of the basic concept in object
orientation in which a new class is created by
absorbing an existing classβs members and
embellishing them with new or modified capabilities.
β The existing class is called the superclass, and the
new class is the subclass.
Syntax for inheritance
class SubClassName extends SuperClassName{};
Why And When To Use "Inheritance"?
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {
// Create a myCar object
Car myCar = new Car();
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
"thanks all!π"Qs: In java , which key word is used to show inheritance in a class?
Anonymous Quiz
14%
super
9%
final
61%
extend
13%
implements
4%
NONE
β€1
EvoNext
LESSON 13: INHERITANCE βοΈInheritance is one of the basic concept in object orientation in which a new class is created by absorbing an existing classβs members and embellishing them with new or modified capabilities. β
The existingβ¦
LESSON 14: ABSTRACTION and INTERFACES
π± Abstraction is the property by virtue of which only the essential details are displayed to the user. The trivial or the non-essential units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components
β³οΈAn Interface in Java programming language is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a class. A Java interface contains static constants and abstract methods.
βοΈWhy do we use an Interface?β
> It is used to achieve total abstraction.
Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances.
> It is also used to achieve loose coupling.
> Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes?
π± Abstraction is the property by virtue of which only the essential details are displayed to the user. The trivial or the non-essential units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components
β³οΈAn Interface in Java programming language is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a class. A Java interface contains static constants and abstract methods.
βοΈWhy do we use an Interface?β
> It is used to achieve total abstraction.
Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances.
> It is also used to achieve loose coupling.
> Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes?
π1
Q!: Which two of the following are legal declarations for non-nested interfaces?
Anonymous Quiz
32%
public abstract class Test { }
32%
protected interface Test { }
25%
abstract public class Test{ }
12%
None
π2
What will be the output of the following program?
public class King {
public static void main(String[] args) {
King k = new King();
Elephant e = k.new Elephant();
System.out.print("Output = ");
System.out.print(e.step2(2, 3));
}
interface Queen {
float step2(int low, int high);
}
interface Pawn {
float step3(int a, int b, int c);
}
abstract class Knight implements Queen, Pawn {
}
class Elephant implements Queen {
public float step2(int x, int y) {
return 2;
}
}
}
//
public class King {
public static void main(String[] args) {
King k = new King();
Elephant e = k.new Elephant();
System.out.print("Output = ");
System.out.print(e.step2(2, 3));
}
interface Queen {
float step2(int low, int high);
}
interface Pawn {
float step3(int a, int b, int c);
}
abstract class Knight implements Queen, Pawn {
}
class Elephant implements Queen {
public float step2(int x, int y) {
return 2;
}
}
}
//
π2π1π₯1
Q2:Which of the following can't be a member of an interface?
Anonymous Quiz
24%
constructors
25%
static methods
30%
non static data members
21%
All of these
π2π1π1π’1
EvoNext
LESSON 14: ABSTRACTION and INTERFACES π± Abstraction is the property by virtue of which only the essential details are displayed to the user. The trivial or the non-essential units are not displayed to the user. Ex: A car is viewed as a car rather than itsβ¦
LESSON 15: πFile Class in javaπ
πThe File class is an abstract representation of file and directory pathname. A pathname can be either absolute or relative.
πOnce you have File object in hand, then there is a list of helper methods which can be used to manipulate the files.
Eg: public String getName()
public String getParent()
public File getParentFile()
public boolean isAbsolute() ... and etc.
πThe File class is an abstract representation of file and directory pathname. A pathname can be either absolute or relative.
πOnce you have File object in hand, then there is a list of helper methods which can be used to manipulate the files.
Eg: public String getName()
public String getParent()
public File getParentFile()
public String getPath()public boolean isAbsolute() ... and etc.
import java.io.*;
public class LearnTcode {
public static void main(String[] args) {
try {
File file = new File("learnTocode.txt");
if (file.createNewFile()) {
System.out.println("Join our channel");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//output is: Join our channelβ€2π₯°1
EvoNext
LESSON 15: πFile Class in javaπ πThe File class is an abstract representation of file and directory pathname. A pathname can be either absolute or relative. πOnce you have File object in hand, then there is a list of helper methodsβ¦
LESSON 16: Java Operator Precedence
> Operator precedence determines the order in which the operators in an expression are evaluated.
> the operator with the highest precedence is operated first.
> the multiplication is performed before subtraction
> Example: Operator precedence
For any suggestion : use our bot_ Learn+To+Code_bot
> Operator precedence determines the order in which the operators in an expression are evaluated.
> the operator with the highest precedence is operated first.
> the multiplication is performed before subtraction
> Example: Operator precedence
class Precedence {
public static void main(String[] args) {
int a = 10, b = 5, c = 1, result;
result = a-++c-++b;
System.out.println(result);
}
}
βΌοΈWhat will be the output of the above exampleβFor any suggestion : use our bot_ Learn+To+Code_bot
π2π₯°2π₯1
public class OperatorPrecedence {
public static void main(String[] args) {
int a = 7 * 3 + 24 / 3 - 5; int b = (7 * 3) + (24 / 3) - 5; System.out.print(a + ", " + b); } }
public static void main(String[] args) {
int a = 7 * 3 + 24 / 3 - 5; int b = (7 * 3) + (24 / 3) - 5; System.out.print(a + ", " + b); } }
Anonymous Quiz
6%
33,33
14%
33,24
56%
24,24
17%
compiler error
7%
Runtime error
π5π1π₯°1
βοΈπ Eid Mubarak βοΈπ
----------------------
To All Muslim Members of our Family, LEARN TO CODE wishes you a Happy Eid Al-Adha (Arefa)β¦!!
ααα«α α¨α’α΅ α α α α΅α α α α...!!
AYYAANA GAARII..!!
----------------------
To All Muslim Members of our Family, LEARN TO CODE wishes you a Happy Eid Al-Adha (Arefa)β¦!!
ααα«α α¨α’α΅ α α α α΅α α α α...!!
AYYAANA GAARII..!!
π4β1π1π₯°1
EvoNext
LESSON 16: Java Operator Precedence > Operator precedence determines the order in which the operators in an expression are evaluated. > the operator with the highest precedence is operated first. > the multiplication is performed before subtraction > Example:β¦
LESSON 17: EXCEPTION HANDLING IN JAVA
π Exception handling is one of the most important feature of java programming that allows us to handle the runtime errors caused by exceptions.
βοΈAn Exception is an unwanted event that interrupts the normal flow of the program.
Java try and catch
The try statement allows you to define a block of code to be tested for errors while it is being executed.
The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);//this num is not found in the array
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}
THERE ARE 2 PRIMARY EXCEPTION TYPES:
1οΈβ£ Checked Exception
2οΈβ£ Unchecked Exception
Contact_bot Β» https://t.me/LearnToCode_bot
π Exception handling is one of the most important feature of java programming that allows us to handle the runtime errors caused by exceptions.
βοΈAn Exception is an unwanted event that interrupts the normal flow of the program.
Java try and catch
The try statement allows you to define a block of code to be tested for errors while it is being executed.
The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);//this num is not found in the array
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}
THERE ARE 2 PRIMARY EXCEPTION TYPES:
1οΈβ£ Checked Exception
2οΈβ£ Unchecked Exception
Contact_bot Β» https://t.me/LearnToCode_bot
Q1:When an improper or erroneous argument is supplied to a method, βββ exception occurs.
Anonymous Quiz
16%
ArrayIndexOutOfBoundsException
8%
NullPointerException
66%
IllegalArgumentException
10%
NumberFormatException
π1
Q2:When you try to retrieve an object using a reference variable whose current value is empty, you'll get__________ exception.
Anonymous Quiz
66%
NullPointerException
12%
ArrayIndexOutOfBoundsException
16%
IllegalArgumentException
6%
NumberFormatException
π1π€1