Java Abstract Class

  1. Create a Simple Abstract Class & Abstract Method in Java
  2. Create Multiple Abstarct Methods within a Abstract Class in Java
  3. Create Multiple Abstarct Class & Abstract Methods in Java
  4. Access Abstarct Class Methods within a Package in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Create a Simple Abstract Class & Abstract Method in Java


AbstractClass_Ex1.java

public abstract class AbstractClass_Ex1 {
 
	abstract void function1();
	
	void function2() {
		
		System.out.println("Function - 2");
	}
}
 
class SubClass extends AbstractClass_Ex1 {
 
	@Override
	void function1() {
		
		System.out.println("Function - 1");
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		SubClass obj = new SubClass();
		
		obj.function1();
		obj.function2();
	}
}

Sample Output

Function - 1
Function - 2




B. Create Multiple Abstarct Methods within a Abstract Class in Java


AbstractClass_Ex2.java

public abstract class AbstractClass_Ex2 {
 
	abstract void get(int a, int b);
	abstract void add();
	abstract void display();
}
 
class SubClass extends AbstractClass_Ex2 {
 
	int x, y, z;
	
	@Override
	void get(int a, int b) {
		// TODO Auto-generated method stub
		
		x = a;
		y = b;
	}
 
	@Override
	void add() {
		// TODO Auto-generated method stub
		
		z = x + y;
	}
 
	@Override
	void display() {
		// TODO Auto-generated method stub
		
		System.out.println("The Addition is : " + z);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		SubClass obj = new SubClass();
		
		obj.get(10, 20);
		obj.add();
		obj.display();
	}
}

Sample Output

The Addition is : 30




C. Create Multiple Abstarct Class & Abstract Methods in Java


AbstractClass_Ex3.java

public abstract class AbstractClass_Ex3 {
 
	abstract void getName(String name);
	abstract void getSex(String sex);
}
 
abstract class AbstClass extends AbstractClass_Ex3 {
	
	abstract void getCity(String city);
	abstract void getCountry(String country);
}
 
class SubClass extends AbstClass {
 
	String name, sex, city, country;
	
	@Override
	void getName(String name) {
		// TODO Auto-generated method stub
		
		this.name = name;
	}
 
	@Override
	void getSex(String sex) {
		// TODO Auto-generated method stub
		
		this.sex = sex;
	}
	
	@Override
	void getCity(String city) {
		// TODO Auto-generated method stub
		
		this.city = city;
	}
 
	@Override
	void getCountry(String country) {
		// TODO Auto-generated method stub
		
		this.country = country;
	}
	
	void display() {
		
		System.out.println("Name : " + name);
		System.out.println("Sex : " + sex);
		System.out.println("City : " + city);
		System.out.println("Country : " + country);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		SubClass obj = new SubClass();
		
		obj.getName("Balaji");
		obj.getSex("Male");
		obj.getCity("Erode");
		obj.getCountry("India");
		
		obj.display();
	}
}

Sample Output

Name : Balaji
Sex : Male
City : Erode
Country : India




D. Access Abstarct Class Methods within a Package in Java


AbstClass.java

package balaji.abst;
 
public abstract class AbstClass {
 
	abstract void getVal(String name);
	abstract void display();
}

MainClass.java

package balaji.abst;
 
public class MainClass extends AbstClass {
	
	String name;
	
	@Override
	void getVal(String name) {
		// TODO Auto-generated method stub
		
		this.name = name;
	}
 
	@Override
	void display() {
		// TODO Auto-generated method stub
		
		System.out.println("Hello " + name);
	}
}
 
class Class {
	
	public static void main(String args[]) {
		
		MainClass obj = new MainClass();
		
		obj.getVal("SampleCodez");
		obj.display();
	}
}

Sample Output

Hello SampleCodez






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com