Skip to main content

Posts

Showing posts from January, 2024

Interface in Java Simple Explanation

  Certainly! Here are some questions related to Java interfaces: What is an interface in Java? Answer: An interface in Java is a collection of abstract methods (methods without a body) and constant variables. It defines a contract for classes that implement it, specifying the methods they must provide. Can a class implement multiple interfaces in Java? Answer: Yes, a class in Java can implement multiple interfaces. This feature is known as multiple inheritance through interfaces. What is the difference between an interface and an abstract class? Answer: An interface can only contain abstract methods and constants, while an abstract class can have both abstract and concrete methods. A class can implement multiple interfaces, but it can extend only one abstract class. Can an interface have fields? Answer: Yes, starting from Java 8, interfaces can have static final fields (constants) and default methods (methods with a default implementation). Explain the purpose of the default m...

Inheritance

  Create a base class Vehicle with properties like model, year, and a method to display  vehicle information. Create a derived class Car that inherits from Vehicle and adds specific properties for a car (e.g., brand).Demonstrate the use of both classes. package mypack; public class Vehicle { public String model ; public int year ; public Vehicle (String model, int year) { this . model = model; this . year = year; } package mypack; import java.util.Scanner; public class Car extends Vehicle { public String brand ; public Car (String model, int year, String brand) { super (model, year); this . brand = brand; } public void display (){ System. out .println( " Model " + model ); System. out .println( " year " + year ); System. out .println( " Brand " + brand ); } public static void main (String [] args){ Scanner scanner = new Scanner(System. in ); Sys...

Accesss Modifiers Java

  Access modifiers in Java are keywords that determine the visibility and accessibility of classes, fields, methods, and other members within a Java program. There are four main access modifiers in Java: a) Private : Have access within same class b)Public : No Restriction on Access c) Protected: Have access within same Package //Q)Discuss the importance of access modifiers in Java classes. // Provide an example where using private and public access modifiers makes sense.(Toyota,Camry) package mypack; public class Car { private String model ; public String brand ; public Car (String model, String brand) { this . model = model; this . brand = brand; } public String getModel () { return model ; } } package mypack; public class CarOwner { public static void main (String[] args) { Car myCar = new Car( "Toyota" , "Camry" ); // Accessing public members directly System. out .println( "Brand: ...

Some Basic Java Practical Questions Asked i Exams

 1) Create a class representing a Book with instance variables for title, author, and price. Include methods to display book details and set the price. Create an object of the class and demonstrate its use. package mypack; import java.util.Scanner; public class Array {     private String title;     private String author;     private Float  price;     public Array(String title,String author,Float price){         this.title=title;         this.price =price;         this.author=author;     }     public void display(){         System.out.println(" The title is " +title);         System.out.println("The author name is " +author);         System.out.println("The price of book is " +price);     }     public void setPrice(Float price){         this.price = price; ...

Java Basic Questions

  Certainly! Here are some commonly asked questions related to classes and objects in Java: What is a class in Java? Answer: A class in Java is a blueprint or a template for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of the class will have. How do you declare a class in Java? Answer: To declare a class in Java, you use the class keyword, followed by the class name and the class body enclosed in curly braces. For example: java Copy code public class MyClass { // class members (fields, methods, etc.) go here } What is an object in Java? Answer: An object in Java is an instance of a class. It is a runtime entity with attributes and behaviors as defined by its class. How do you create an object in Java? Answer: To create an object in Java, you use the new keyword, followed by the class constructor. For example: java Copy code MyClass myObject = new MyClass (); What is the constructor in Java? Answer: A constructor in...