Diferences-Advanced Computer Programminng-Lab Lecture, Lecture notes of Java Programming

This lecture was delivered by Prof. Mudita Tiwari at Cochin University of Science and Technology for Java Programming course. It includes: Implementation, Signature, Class, Interfaces, Modifiers, Public, Behavior, Console

Typology: Lecture notes

2011/2012

Uploaded on 07/19/2012

rohit-sharma
rohit-sharma 🇮🇳

4.3

(11)

200 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ABSTRACT CLASS
INTERFACE
Cannot be instantiated but it may provide
implementation of some of its methods. At least
implementation of one method is not specified.
This method has to be overridden.
Cannot provide any implementation. It just
provides the signature.
Only one class may inherit from only one class
Many interfaces may be implemented
Access modifiers may be specified in an abstract
class
All methods and properties are public. We cannot
specify the access modifiers by ourselves
If various implementations are of the same kind
and use common behavior then abstract class is
better to use.
If various implementations only share method
signatures then it is better to use interface
public interface IVehicle
{
void Start();
void Drive();
void Park();
void ChangeGear(int gear);
}
public class Vehicle : IVehicle
{
public void Start()
{
Console.WriteLine("The vehicle has been started");
}
public void Drive()
{
Console.WriteLine("The vehicle is being driven");
}
public void Park()
{
Console.WriteLine("The vehicle is being parked");
}
public void ChangeGear(int gear)
{
Console.WriteLine("Gear changed to " + gear.ToString());
}
}
public abstract class Vehicle
{
public void Start()
docsity.com
pf2

Partial preview of the text

Download Diferences-Advanced Computer Programminng-Lab Lecture and more Lecture notes Java Programming in PDF only on Docsity!

ABSTRACT CLASS INTERFACE

Cannot be instantiated but it may provide

implementation of some of its methods. At least

implementation of one method is not specified.

This method has to be overridden.

Cannot provide any implementation. It just

provides the signature.

Only one class may inherit from only one class Many interfaces may be implemented

Access modifiers may be specified in an abstract

class

All methods and properties are public. We cannot

specify the access modifiers by ourselves

If various implementations are of the same kind

and use common behavior then abstract class is

better to use.

If various implementations only share method

signatures then it is better to use interface

public interface IVehicle { void Start(); void Drive(); void Park(); void ChangeGear(int gear); } public class Vehicle : IVehicle { public void Start() { Console.WriteLine("The vehicle has been started"); } public void Drive() { Console.WriteLine("The vehicle is being driven"); } public void Park() { Console.WriteLine("The vehicle is being parked"); } public void ChangeGear(int gear) { Console.WriteLine("Gear changed to " + gear.ToString()); } } public abstract class Vehicle { public void Start()

docsity.com

Console.WriteLine("The vehicle has been started"); } public abstract void Drive(); public abstract void Park(); public abstract void ChangeGear(int gear); public void SwitchOff() { Console.WriteLine("The vehicle has been switched off"); } } public class Car : Vehicle { public Car() { } public override void Drive() { Console.WriteLine("The car is being driven"); } public override void Park() { Console.WriteLine("The car is being parked"); } public override void ChangeGear(int gear) { Console.WriteLine("The car changed gear changed to " + gear.ToString()); } }

docsity.com