OOPs Assignment
OOPs Assignment
// Abstracted class
class Dog extends Animal {
public Dog(String name) { super(name); }
// Abstracted class
class Cat extends Animal {
public Cat(String name) { super(name); }
// Driver Class
public class AbstractionExample {
// Main Function
public static void main(String[] args)
{
Animal myDog = new Dog("Buddy");
Animal myCat = new Cat("Fluffy");
myDog.makeSound();
myCat.makeSound();
}
}
Ques 7. What is the Difference between an abstract
method and final method in java ? Explain with an
example
ANS - Final Class: A class which is declared with the “Final” keyword
is known as the final class. The final keyword is used to finalize the
implementations of the classes, the methods and the variables used in
this class. The main purpose of using a final class is to prevent the
class from being inherited (i.e.) if a class is marked as final, then no
other class can inherit any properties or methods from the final class. If
the final class is extended, Java gives a compile-time error. The
following is an example of how a final class is declared. However, a
compile-time error is given because this final class is being inherited.
// Final class
final class Super {
private int data = 100;
}
public class Sub extends Super {
public static void main(String args[])
{
}
}
Abstract Class: A class that is declared using the “abstract” keyword is
known as an abstract class. The main idea behind an abstract class is
to implement the concept of Abstraction. An abstract class can have
both abstract methods(methods without body) as well as the concrete
methods(regular methods with the body). However, a normal class(non-
abstract class) cannot have abstract methods. The following is an
example of how an abstract class is declared.
// an abstract class
// Driver code
public static void main(String args[])
{
Book obj = new shayar();
obj.page();
}
}
Abstraction is a feature of OOPs Encapsulation is also a feature of OOPs. It hides the code
that hides and data into a single entity or unit so that the data can
the unnecessary detail but be protected from the outside world.
shows the essential information.
In abstraction, we use abstract We use the getters and setters methods to hide the
classes and interfaces to hide data.
the code complexities.
// Driver code
public static void main(String args[])
{
// Here, the first addition
// function is called
System.out.println(add(2, 3));
// Implementing a method
public void method()
{
System.out.println("Method 1");
}
}
// Driver code
public static void main(String args[])
{
Test test = new GFG();
test.method();
}
}
Compile Time Polymorphism Run time Polymorphism