Sie sind auf Seite 1von 4

Yash R Jain

171080055
T.Y. B.Tech I.T.

Assignment 3
Abstraction
Abstraction allows you to view things in more general terms rather than looking them as they are at
the moment, which gives your code flexibility to deal with the changes coming in future.

For example, if you were to design a program to control vehicles e.g. starting, stopping, horn,
accelerator, breaks etc, how do you do that? would you design your program just to work with car or
bike or would you think about different kinds of vehicles? This is where Abstraction comes into the
picture, it allows you think in terms of Vehicle rather than thinking in terms of Car. It provides that
generalization much needed for a software to be reusable and customizable.

Abstraction is nothing but creating a different class to handle details up to certain level. Class with a
higher level of Abstraction will deal with most general details and classes at the lowest level of
Abstraction will deal with most specific details.

At the top of Abstraction, we have complete abstract things which don't do anything by itself but
specify contracts about how things will work.

At runtime we connect abstract things with a concrete implementation. This allows you to code with
abstract class and interface, which results in much more flexible and maintainable code then directly
working with concrete classes. At runtime, OOP identifies which concrete class's object is available
and then it calls the implementation of the abstract method from that class.

Example:

public abstract class Vehicle

abstract void printDescription();

public class Car extends Vehicle

void printDescription(){

System.out.println("Four Wheels");

public static void main(String args[]){

Vehicle o=new Car();

o.printDescription();

}
Yash R Jain
171080055
T.Y. B.Tech I.T.
}

Ouput:

Polymorphism
The word polymorphism is used in various contexts and describes situations in which something
occurs in several different forms. In computer science, it describes the concept that objects of
different types can be accessed through the same interface. Each type can provide its own,
independent implementation of this interface.

OOP has two kinds of polymorphism. You can overload a method with different sets of parameters.
This is called static polymorphism because the compiler statically binds the method call to a specific
method.

Within an inheritance hierarchy, a subclass can override a method of its superclass. If you instantiate
the subclass, the JVM will always call the overridden method, even if you cast the subclass to its
superclass. That is called dynamic polymorphism.

Example for overloading

public class CalcArea

double area(double r)////overloaded method 1

double a=3.14*r*r;
Yash R Jain
171080055
T.Y. B.Tech I.T.
return a;

int area(int h,int b)//overloaded method 2

int a=h*b;

return a;

public static void main(String args[]){

CalcArea c=new CalcArea();

System.out.println("Area of circle="+c.area(5.0));//since only one parameter is passed the 1st


method is called

System.out.println("Area of rect="+c.area(5,6));//since only one parameter is passed the 2nd


method is called

Output:

Example for overriding:

public class Override {

public static void main(String[] args) {

A ref1 = new B();//object methods refer to B

B ref2 = new C();//object method refer class C


Yash R Jain
171080055
T.Y. B.Tech I.T.
System.out.println(“f=”+ref1.f());

System.out.println(“f=”+ref2.f());

class A { int f() { return 0; } }

class B extends A {

int f() { return 1; }//same defination of f().Hence overriden

class C extends B {

int f() { return 2; }// //same defination of f().Hence overriden

Output:

Das könnte Ihnen auch gefallen