Java Inheritance Update 2023 With Program And Example nheritance is one of the essential concepts in object-orientated programming
(OOP) that lets in you to create a new magnificence this is based on an current elegance.
Inheritance allows you to define a brand new elegance (known as a subclass or derived magnificence) via inheriting
The homes and behaviors (fields and strategies) of an existing class (called a superclass or base magnificence).
This allows for code reuse and the introduction of a hierarchy of instructions.
Here's a simple Java program that demonstrates inheritance: // Superclass (Base class)
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void eat() {
System.out.println(name + " is eating.");
}
void sleep() {
System.out.println(name + " is sleeping.");
}
}
// Subclass (Derived class)
class Dog extends Animal {
Dog(String name) {
super(name); // Call the constructor of the superclass
}
void bark() {
System.out.println(name + " is barking.");
}
}
// Subclass (Derived class)
class Cat extends Animal {
Cat(String name) {
super(name); // Call the constructor of the superclass
}
void meow() {
System.out.println(name + " is meowing.");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
Cat cat = new Cat("Whiskers");
dog.eat();
dog.sleep();
dog.bark();
cat.eat();
cat.sleep();
cat.meow();
}
} 👇👇👇👇👇👇 https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html
(OOP) that lets in you to create a new magnificence this is based on an current elegance.
Inheritance allows you to define a brand new elegance (known as a subclass or derived magnificence) via inheriting
The homes and behaviors (fields and strategies) of an existing class (called a superclass or base magnificence).
This allows for code reuse and the introduction of a hierarchy of instructions.
Here's a simple Java program that demonstrates inheritance: // Superclass (Base class)
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void eat() {
System.out.println(name + " is eating.");
}
void sleep() {
System.out.println(name + " is sleeping.");
}
}
// Subclass (Derived class)
class Dog extends Animal {
Dog(String name) {
super(name); // Call the constructor of the superclass
}
void bark() {
System.out.println(name + " is barking.");
}
}
// Subclass (Derived class)
class Cat extends Animal {
Cat(String name) {
super(name); // Call the constructor of the superclass
}
void meow() {
System.out.println(name + " is meowing.");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
Cat cat = new Cat("Whiskers");
dog.eat();
dog.sleep();
dog.bark();
cat.eat();
cat.sleep();
cat.meow();
}
} 👇👇👇👇👇👇 https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html
👍2
import java.util.Arrays;
import java.util.LinkedHashSet;
public class ArrayExample
{
public static void main(String[] args) throws CloneNotSupportedException
{
//Array with duplicate elements
Integer[] numbers = new Integer[] {1,2,3,4,5,1,3,5};
//This array has duplicate elements
System.out.println( Arrays.toString(numbers) );
//Create set from array elements
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>( Arrays.asList(numbers) );
//Get back the array without duplicates
Integer[] numbersWithoutDuplicates = linkedHashSet.toArray(new Integer[] {});
//Verify the array content
System.out.println( Arrays.toString(numbersWithoutDuplicates) );
}
}
import java.util.LinkedHashSet;
public class ArrayExample
{
public static void main(String[] args) throws CloneNotSupportedException
{
//Array with duplicate elements
Integer[] numbers = new Integer[] {1,2,3,4,5,1,3,5};
//This array has duplicate elements
System.out.println( Arrays.toString(numbers) );
//Create set from array elements
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>( Arrays.asList(numbers) );
//Get back the array without duplicates
Integer[] numbersWithoutDuplicates = linkedHashSet.toArray(new Integer[] {});
//Verify the array content
System.out.println( Arrays.toString(numbersWithoutDuplicates) );
}
}
❤1👍1
Java Interview Programs:
2)Fibonacci Series using recursion in java
class FibonacciExample2
{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count)
{
if(count>0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
printFibonacci(count-1);
}
}
public static void main(String args[])
{
int count=10;
System.out.print(n1+" "+n2);
//printing 0 and 1
printFibonacci(count-2);
//n-2 because 2 numbers are already printed
}
}
2)Fibonacci Series using recursion in java
class FibonacciExample2
{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count)
{
if(count>0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
printFibonacci(count-1);
}
}
public static void main(String args[])
{
int count=10;
System.out.print(n1+" "+n2);
//printing 0 and 1
printFibonacci(count-2);
//n-2 because 2 numbers are already printed
}
}
❤1👍1
Java Interview Programs:
!) Fibonacci Series in Java without using recursion
class FibonacciExample1
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);
//printing 0 and 1
for(i=2;i<count;++i)
//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
!) Fibonacci Series in Java without using recursion
class FibonacciExample1
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);
//printing 0 and 1
for(i=2;i<count;++i)
//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
❤2
What do you understand by copy constructor in Java?
There is no copy constructor in java. However, we can copy the values from 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
In this example, we are going to copy the values of one object into another using java constructor.
https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html
There is no copy constructor in java. However, we can copy the values from 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
In this example, we are going to copy the values of one object into another using java constructor.
https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html
❤1
What is JDBC?
JDBC is a Java API that is used to connect and execute the query to the database. JDBC API uses JDBC drivers to connect to the database. JDBC API can be used to access tabular data stored into any relational database.
JDBC is a Java API that is used to connect and execute the query to the database. JDBC API uses JDBC drivers to connect to the database. JDBC API can be used to access tabular data stored into any relational database.
What are the JDBC statements?
In JDBC, Statements are used to send SQL commands to the database and receive data from the database. There are various methods provided by JDBC statements such as execute(), executeUpdate(), executeQuery, etc. which helps you to interact with the database.
In JDBC, Statements are used to send SQL commands to the database and receive data from the database. There are various methods provided by JDBC statements such as execute(), executeUpdate(), executeQuery, etc. which helps you to interact with the database.
👍1
What is the return type of Class.forName() method?
The Class.forName() method returns the object of java.lang.Class object
The Class.forName() method returns the object of java.lang.Class object
👍1
For more information visit
https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html
https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html
How can we set null value in JDBC PreparedStatement?
By using setNull() method of PreparedStatement interface, we can set the null value to an index. The syntax of the method is given below.
void setNull(int parameterIndex, int sqlType) throws SQLException
By using setNull() method of PreparedStatement interface, we can set the null value to an index. The syntax of the method is given below.
void setNull(int parameterIndex, int sqlType) throws SQLException
What is the role of the JDBC DriverManager class?
The DriverManager class acts as an interface between user and drivers. It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method DriverManager.registerDriver().
The DriverManager class acts as an interface between user and drivers. It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method DriverManager.registerDriver().
What are the functions of the JDBC Connection interface?
The Connection interface maintains a session with the database. It can be used for transaction management. It provides factory methods that return the instance of Statement, PreparedStatement, CallableStatement, and DatabaseMetaData.
The Connection interface maintains a session with the database. It can be used for transaction management. It provides factory methods that return the instance of Statement, PreparedStatement, CallableStatement, and DatabaseMetaData.
What is the difference between HashSet and TreeSet?
The HashSet and TreeSet, both classes, implement Set interface. The differences between the both are listed below.
HashSet maintains no order whereas TreeSet maintains ascending order.
HashSet impended by hash table whereas TreeSet implemented by a Tree structure.
HashSet performs faster than TreeSet.
HashSet is backed by HashMap whereas TreeSet is backed by TreeMap.
The HashSet and TreeSet, both classes, implement Set interface. The differences between the both are listed below.
HashSet maintains no order whereas TreeSet maintains ascending order.
HashSet impended by hash table whereas TreeSet implemented by a Tree structure.
HashSet performs faster than TreeSet.
HashSet is backed by HashMap whereas TreeSet is backed by TreeMap.
👍3
What is the difference between Set and Map?
The differences between the Set and Map are given below.
Set contains values only whereas Map contains key and values both.
Set contains unique values whereas Map can contain unique Keys with duplicate values.
Set holds a single number of null value whereas Map can include a single null key with n number of null values.
The differences between the Set and Map are given below.
Set contains values only whereas Map contains key and values both.
Set contains unique values whereas Map can contain unique Keys with duplicate values.
Set holds a single number of null value whereas Map can include a single null key with n number of null values.
What is the difference between HashSet and HashMap?
The differences between the HashSet and HashMap are listed below.
HashSet contains only values whereas HashMap includes the entry (key, value). HashSet can be iterated, but HashMap needs to convert into Set to be iterated.
HashSet implements Set interface whereas HashMap implements the Map interface
HashSet cannot have any duplicate value whereas HashMap can contain duplicate values with unique keys.
HashSet contains the only single number of null value whereas HashMap can hold a single null key with n number of null values.
The differences between the HashSet and HashMap are listed below.
HashSet contains only values whereas HashMap includes the entry (key, value). HashSet can be iterated, but HashMap needs to convert into Set to be iterated.
HashSet implements Set interface whereas HashMap implements the Map interface
HashSet cannot have any duplicate value whereas HashMap can contain duplicate values with unique keys.
HashSet contains the only single number of null value whereas HashMap can hold a single null key with n number of null values.
👍2
What is the difference between HashMap and TreeMap?
The differences between the HashMap and TreeMap are given below.
HashMap maintains no order, but TreeMap maintains ascending order.
HashMap is implemented by hash table whereas TreeMap is implemented by a Tree structure.
HashMap can be sorted by Key or value whereas TreeMap can be sorted by Key.
HashMap may contain a null key with multiple null values whereas TreeMap cannot hold a null key but can have multiple null values.
The differences between the HashMap and TreeMap are given below.
HashMap maintains no order, but TreeMap maintains ascending order.
HashMap is implemented by hash table whereas TreeMap is implemented by a Tree structure.
HashMap can be sorted by Key or value whereas TreeMap can be sorted by Key.
HashMap may contain a null key with multiple null values whereas TreeMap cannot hold a null key but can have multiple null values.
What is the difference between Collection and Collections?
The differences between the Collection and Collections are given below.
The Collection is an interface whereas Collections is a class.
The Collection interface provides the standard functionality of data structure to List, Set, and Queue. However, Collections class is to sort and synchronize the collection elements.
The Collection interface provides the methods that can be used for data structure whereas Collections class provides the static methods which can be used for various operation on a collection.
The differences between the Collection and Collections are given below.
The Collection is an interface whereas Collections is a class.
The Collection interface provides the standard functionality of data structure to List, Set, and Queue. However, Collections class is to sort and synchronize the collection elements.
The Collection interface provides the methods that can be used for data structure whereas Collections class provides the static methods which can be used for various operation on a collection.
👍2
For more information visit
https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html
https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html
Forwarded from java interview Questions (Raj patel)
HAS-A Relationship
1.HAS-A Relationship is also knows as composition(or) aggregation.
2.There is no specific keyword to implement HAS-A relationship but mostly
we can use new operator.
3.The main advantage of reusability.
Example:-
class Engine
{
// engine functionality
}
class Car
{
Engine e=new Engine();
//------------------------------;
//------------------------------;
//------------------------------;
}
NOTE:-
class car has-a engine reference.
The main dis-advantage of has-a relationship increases dependency between the
components and creates maintains problems. 👌👌👌👌👌👌 More java interview Questions Visit: -https://cjavapoint.blogspot.com/👈👈👈👈👈
1.HAS-A Relationship is also knows as composition(or) aggregation.
2.There is no specific keyword to implement HAS-A relationship but mostly
we can use new operator.
3.The main advantage of reusability.
Example:-
class Engine
{
// engine functionality
}
class Car
{
Engine e=new Engine();
//------------------------------;
//------------------------------;
//------------------------------;
}
NOTE:-
class car has-a engine reference.
The main dis-advantage of has-a relationship increases dependency between the
components and creates maintains problems. 👌👌👌👌👌👌 More java interview Questions Visit: -https://cjavapoint.blogspot.com/👈👈👈👈👈
👍2❤1