You are on page 1of 2

Class: A class is an entity that determines how an object will behave and what

the object will contain. In other words, it is a blueprint or a set of instruction to


build a specific type of object.
Syntax:
class <class name>
{
field;
method;
}
Object: An object is nothing but a self-contained component which consists of
methods and properties to make a data useful. Object determines the
behaviour of the class. When you send a message to an object, you are
asking the object to invoke or execute one of its methods.
From a programming point of view, an object can be a data structure, a
variable or a function. It has a memory location allocated. The object is
designed as class hierarchies.
Syntax:

ClassName ReferenceVariable = new ClassName()

Example of class and object:


// Class Declaration
public class Dog
{
// Instance Variables
String breed;
String size;
int age;
String colour;
// method 1
public String getInfo()
{
return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" colour is:
"+colour);
}
public static void main(String[] args)
{
Dog maltese = new Dog();
maltese.breed="Maltese";
maltese.size="Small";
maltese.age=2;
maltese.color="white";
System.out.println(maltese.getInfo());
}
}
Inheritance in Java

Inheritance in Java is a mechanism in which one object acquires all the


properties and behaviours of a parent object. It is an important part of OOPs
(Object Oriented programming system). Inheritance represents the IS-A
relationship which is also known as a parent-child relationship
We use inheritance in java for:
o For Method Overriding (so runtime polymorphism can be achieved).
o For Code Reusability.
Overloading and overriding in java

Overloading occurs when two or more methods in one class have the same
method name but different parameters.
Example:
class Dog
{
public void bark()
{
System.out.println("woof ");
}

//overloading method
public void bark(int num)
{
for(int i=0; i<num; i++)
System.out.println("woof ");
}
}
Overriding means having two methods with the same method name and
parameters (i.e., method signature). One of the methods is in the parent class
and the other is in the child class. Overriding allows a child class to provide a
specific implementation of a method that is already provided its parent class.
Example:
class Dog
{
public void bark()
{
System.out.println("woof ");
}
}
class Hound extends Dog
{
public void sniff()
{
System.out.println("sniff ");
}

public void bark()


{
System.out.println("bowl");
}
}

public class OverridingTest


{
public static void main(String [] args)
{
Dog dog = new Hound();
dog.bark();
}
}
Throw vs Throws in java

1. Throws clause is used to declare an exception, which means it works


similar to the try-catch block. On the other hand, throw keyword is used to
throw an exception explicitly.
2. If we see syntax wise than throw is followed by an instance of Exception
class and throws is followed by exception class names.
3. Throw keyword is used in the method body to throw an exception, while
throws are used in method signature to declare the exceptions that can occur
in the statements present in the method.
4. You can throw one exception at a time, but you can handle multiple
exceptions by declaring them using throws keyword.

Throw Example

public class Example1


{
void checkAge(int age)
{
if(age<18)
throw new ArithmeticException("Not Eligible for voting");
else
System.out.println("Eligible for voting");
}
public static void main(String args[])
{
Example1 obj = new Example1();
obj.checkAge(13);
System.out.println("End Of Program");
}
}
Throws Example

public class Example1


{
int division (int a, int b) throws ArithmeticException
{
int t = a/b;
return t;
}
public static void main(String args[])
{
Example1 obj = new Example1();
Try
{
System.out.println(obj.division(15,0));
}
catch(ArithmeticException e)
{
System.out.println("You shouldn't divide number by zero");
}
}
}

You might also like