Sie sind auf Seite 1von 4

What is an abstract class, and when should it be used?

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods. Example:
public abstract Animal { public void eat(Food food) { // do something with food.... } public void sleep(int hours) { try { // 1000 milliseconds * 60 seconds * 60 minutes * hours Thread.sleep ( 1000 * 60 * 60 * hours); } catch (InterruptedException ie) { /* ignore */ } } public abstract void makeNoise(); } public Dog extends Animal { public void makeNoise() { System.out.println ("Bark! Bark!"); } } public Cow extends Animal { public void makeNoise() { System.out.println ("Moo! Moo!"); } } What is Method Overriding? Method overriding is when the function of base class is re-defined with the same name, function signature and access specifier (either public or protected) of the derived class. What is Overloading? Overloading is polymorphism. In this we use more than one same name function but with different parameter value.For example: read(int a){ ........... } read(int a, int b){ ........ }

What is Inheritance? Inheritance is the process by which a class acquires the attributes and properties of another class. Types of Inheritance 1. Single Inheritance 2. Multiple Inheritance 3. Multilevel Inheritance 4. Hierarchial Inheritance 1. Single Inheritance - One base class and one derived class. 2. Multiple Inheritance - A class is derived from more than one base classes 3. Multilevel Inheritance - A sub class inherits from a class which inherits from another class. 4. Hierarchical Inheritance - More than one subclass inherited from a single base class. What is encapsulation? Store data and function in a single unit is called as encapsulation. What is polymorphism? Generally, it is the ability to appear in different forms. In oops concept, it is the ability to process objects differently depending on their data types. Its the ability to redefine methods for derived classes. There are two types of Polymorphism namely: Compile Time Polymorphism Run Time Polymorphism Compile Time Polymorphism is achieved through function overloading and operator overloading Run Time Polymorphism is achieved through virtual Function.
1. Static Polymorphism public Class StaticPolyDemo { public void display(int x) { Console.WriteLine(Area of a Square:+x*x); } public void display(int x, int y) { Console.WriteLine(Area of a Square:+x*y); } public static void main(String args[])

StaticPolyDemo spd=new StaticPolyDemo(); Spd.display(5); Spd.display(10,3);

2. Dyanamic Polymorphism Class Test { Public virtual void show() { Console.WriteLine(From base class show method); } } Public Class DynamicPolyDemo : Test { Public override void show() { Console.WriteLine(From Derived Class show method); } Public static void main(String args[]) { DynamicPolyDemo dpd=new DynamicPolyDemo(); Dpd.show(); } }

Difference between Encapsulation and Abstraction


1. Abstraction solves the problem in the 1. Encapsulation solves the problem in the design level implementation level 2. Encapsulation means hiding the code and data 2. Abstraction is used for hiding the in to a single unit to protect the data from outside unwanted data and giving relevant data world 3. Abstraction is a technique that helps 3. Encapsulation is the technique for packaging to identify which specific information the information in such a way as to hide what should be visible and which information should be hidden, and make visible what is should be hidden. intended to be visible. What is the difference between Abstract and Interface?

In abstract class public,private and protected method can used. In Interface only public method can used. Abstract Class may contain constructor but interface does not contain constructor.

Das könnte Ihnen auch gefallen