Lecture 3 Flashcards
How do you create a class in Java
class NameofClass{
}
Chaterstics of a class
Constructors always have the same name as the class.
Constructors don’t have a return type.
Constructor is a block of code that can be used to initializes the newly created object.
A constructor resembles an instance method in java but it’s not a method as it doesn’t have a return type.
Give an example of a constructor syntax
class NameofClass{
NameofClass();
}
Mention types of constructor
- Default constructor
If you do not implement any constructor in your class, Java compiler inserts a default constructor into your code on your behalf at compilation time.
This constructor is known as default constructor. - no-arg constructor:
Constructor with no arguments is known as no-arg constructor. The signature is same as default constructor, however body can have any code unlike default constructor where the body of the constructor is empty. - Parameterized constructor
Constructor with arguments(or you can say parameters) is known as Parameterized constructor.
What is constructor overloading
Constructor overloading is a concept of having more than one constructor with different parameters so that every constructor can perform a different task.
What is a method
A method is a collection or a block of statements that perform some specific task and return the result to the caller or not.
What is method of overloading
Method Overloading is a feature that allows a class to have multiple methods with the same name but with different number, sequence or type of parameters.
Difference between constructor and methods
A Constructor is invoked implicitly by the system while a Method is invoked by the programmer
Difference between classes and interface
In Java, a class is a blueprint or template for creating objects. It defines the properties (fields or attributes) and behaviors (methods or functions) that all objects of that class will have.
On the other hand, an interface in Java defines a contract for classes to follow. It specifies a set of method signatures (without implementations) that any class implementing the interface must provide. Think of it as a promise that any class implementing the interface will provide certain functionality.
An example of interface and class
// Define the Animal interface
interface Animal {
void makeSound();
}
// Define the Dog class implementing the Animal interface
class Dog implements Animal {
@Override
public void makeSound() {
System.out.println(“Woof”);
}
}
// Define the Cat class implementing the Animal interface
class Cat implements Animal {
@Override
public void makeSound() {
System.out.println(“Meow”);
}
}
public class Main {
public static void main(String[] args) {
// Create objects of Dog and Cat
Dog myDog = new Dog();
Cat myCat = new Cat();
// Call makeSound method on Dog and Cat objects myDog.makeSound(); // Output: Woof myCat.makeSound(); // Output: Meow } }
Describe java.lang.Runnable
java.lang.Runnable is an interface that is to be implemented by a class whose instances are intended to be executed by a thread.
Major components of AWT
Containers
Canvases: It is a simple drawing interface
UI compoenets
Window construction components