The document discusses abstract classes in Java, which cannot be instantiated and are intended for subclassing. It outlines key features such as abstract methods, regular methods, constructors, fields, and inheritance, along with three examples demonstrating their use. Each example illustrates how abstract classes can define common functionality while allowing subclasses to implement specific behaviors.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views5 pages
Abstraction Assignment DSAI 242102011
The document discusses abstract classes in Java, which cannot be instantiated and are intended for subclassing. It outlines key features such as abstract methods, regular methods, constructors, fields, and inheritance, along with three examples demonstrating their use. Each example illustrates how abstract classes can define common functionality while allowing subclasses to implement specific behaviors.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5
ASSIGNMENT
NAME : ASAD UR REHMAN
CLASS : DASC (2A)
REG NO. DSA242102011
SUBECT : OBECT ORRIENTED PROGRAMING LAB
SUBMITTED TO : SIR ABDUL WAHAB
TOPIC :ABSTRACT class
Abstraction: In Java, an abstract class is a class that cannot be instantiated on its own and is meant to be subclassed by other classes. It can contain abstract methods (methods without a body) as well as regular methods (methods with implementation). Abstract classes are used when you want to define a base class with common functionality but leave some methods to be implemented by subclasses. Key Features of Abstract Classes: 1. Abstract Methods: An abstract class can contain methods that have no implementation. These methods are declared with the abstract keyword. Subclasses must provide implementations for these methods unless the subclass is also abstract. 2. Regular Methods: An abstract class can also have methods with a full implementation, which can be inherited by subclasses. 3. Constructors: Abstract classes can have constructors, but they cannot be directly instantiated. The constructor is used by subclasses when they are instantiated. 4. Fields/Instance Variables: Abstract classes can have instance variables, and they can be accessed by the subclasses. 5. Inheritance: An abstract class can extend other classes and be extended by concrete or other abstract classes.
Example No,1
abstract class Animal {
public abstract void sound(); } class Dog extends Animal { @Override public void sound() { System.out.println("Bark"); } } public class Main { public static void main(String[] args) { Animal dog = new Dog(); dog.sound(); // Output: Bark } }
Example No,2
abstract class Vehicle {
public void move() { System.out.println("The vehicle is moving."); } public abstract void type(); } class Car extends Vehicle { @Override public void type() { System.out.println("This is a car."); } } public class Main { public static void main(String[] args) { Vehicle car = new Car(); car.move(); // Output: The vehicle is moving. car.type(); // Output: This is a car. } }
Example No,3
abstract class Person {
String name; public Person(String name) { this.name = name; } public abstract void greet(); // Abstract method } class Student extends Person { public Student(String name) { super(name); } @Override public void greet() { System.out.println("Hello, I'm " + name); } } public class Main { public static void main(String[] args) { Person student = new Student("John"); student.greet(); // Output: Hello, I'm John } }