L4-Abstract Classes,Interfaces and OOD principles

Intended Learning Outcomes

  • To understand abstract type.
  • To enforce a design protocol by using abstract classes or interfaces.
  • To know the similarities and differences between an abstract class and an
    interface.
  • To become familiar with the process of program development.
  • To discover classes using CRC cards.
  • To understand the impacts of coupling to a system.
  • To learn the relationship types: association, aggregation, composition,
    realization and generalization.(UML class diagram)
  • To understand design principles and guidelines

Interface Syntax

  • Similar to a class but contains only constants and abstract methods.
  • Using abstract modifier.
java 复制代码
public interface InterfaceName{
	constant declarations;
	method signatures;
	}
public interface Stack{
	public static final int MAX_SIZE=100;
	public abstract int pop();
	public abstract void push(int e);
	}
  • Interface class is a special class:
    • interfaces contain abstract methods which don't define method bodies, you can't create instances from an interface using the new as usual, but you can use an interface to declare a variable.
java 复制代码
Stack s1;//no instance,only reference var
Stack[] s2=new Stack[10];
  • Properties of Interface(these modifiers can be omitted)

    • All Data fields: constant--public static final
    • All methods:abstract--public abstract
  • Using Interface(relying another class to implement the abstract class)

java 复制代码
public interface Colorable{
	abstract public void color();
	}
class Apple implements Colorable{
	@override
	public void color(){
	System.out.println("red");
	}
}
class Banana implements Colorable{
	@override
	public void color(){
	System.out.println("Yellow");
	}
}

The abstract Modifier

  • Abstract also can be used in class
    • Having both ordinary class and interface.
    • Having both abstract and non-abstract method
    • Being extended and the subclass override the abstract method from parent class with a concrete method.
    • Can't be instantiated.(can't use new operator)(can usr reference variable)
java 复制代码
public abstract class shape{
	protected char drawingchar='*';
	public abstract void draw();//abstract method
	public char getDrawingChar(){//non-abstract method
		return drawingchar;
		}
	}
  • Abstract Class(define)

    • an abstract class can contains no abstract methods
    • But a class that includes abstract methods must be an abstract class(but not all abstract)
  • Why Abstract Type?

    Abstract types are useful that they can be used to define and enforce a
    protocol ; a set of operations which all objects that implement the protocol

    must support.

Interfaces VS. Abstract Classes

Abstract Class Interfaces
no restrictions of variables All variables must be public static final
no restrictions of methods All methods must be public abstract
Constructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator. No constructors. An interface cannot be instantiated using the new operator.
single Inheritance,extend one class only multiple inheritance more than one interface

Object-oriented-Design Principles

  • software development process

    • Requirement Specification:understand the problem
    • System analysis: analyze the business process, capture the essential elements
    • System design: decompose the problem, establish relationships
    • Implementation: coding,testing,debugging
    • Testing:ensure the code meet the requirement
    • Deployment: available for use
    • Maintainance: provide upgrades of the product to fix newly discovered bugs
  • CRC code


  • Coupling

    • Association, aggregation, composition, realization and generalization all describe the coupling between two classes.
    • The difference is the degree of coupling.
    • In general, lower degree of coupling implies higher stability of the system.
    • Low coupling is often a desirable system property.
  • Association

    • Object of one class is connecte to object of another and there is a channel between them through which message can be sent.
    • send message each other
    • one change another change too
    • the connection relationship is usually implemented using data fields (Strong connection between 2 classes)
  • Composition and Aggregation

    • they represent a whole-part relationship (between two classes.)
    • whole contains part, but part can't contain whole
    • Composition stronger than Aggregation(because:adds lifetime responsibility--the part must be created and destroyed with whole together.)
    • Usually represented as a data field in the 'owner' class.
  • Inner class

    • If Department is used in the University class only,it is usually declared as an inner class of University.(表示这个子类只属于它,其他类中没有该子类)
    • There are three types of inner class:
  1. Member inner class - declared within another class
  2. Local inner class - declared within the body of a method
  3. Anonymous inner class - declared within the body of a method without name
    example : member inner class.
java 复制代码
public class University { 
 private Department[] depts;
 ...
 private class Department {
 ...
 }
}
  • Generalization (Extend relationship)

    • models the inheritance relationship(between 2 classes)
    1. Generalized class(superclass)
    2. Specialized class(subclass)
  • Realization

    • Represents the is-a-kind-of relationship(Implementation of interface)

Class design in 4 steps

  1. Identify classes for the system.
    Ordinary classes, abstract classes, interfaces
  2. Describe attributes and methods in each class.
    Using Modifiers public, protected, private and static
  3. Establish relationships among classes.
    Association, generalization,realization, ... etc.
  4. Create classes.
相关推荐
苏-言4 分钟前
Spring IOC实战指南:从零到一的构建过程
java·数据库·spring
土豆湿4 分钟前
拥抱极简主义前端开发:NoCss.js 引领无 CSS 编程潮流
开发语言·javascript·css
界面开发小八哥11 分钟前
更高效的Java 23开发,IntelliJ IDEA助力全面升级
java·开发语言·ide·intellij-idea·开发工具
草莓base25 分钟前
【手写一个spring】spring源码的简单实现--容器启动
java·后端·spring
Allen Bright38 分钟前
maven概述
java·maven
qystca40 分钟前
洛谷 B3637 最长上升子序列 C语言 记忆化搜索->‘正序‘dp
c语言·开发语言·算法
编程重生之路40 分钟前
Springboot启动异常 错误: 找不到或无法加载主类 xxx.Application异常
java·spring boot·后端
薯条不要番茄酱40 分钟前
数据结构-8.Java. 七大排序算法(中篇)
java·开发语言·数据结构·后端·算法·排序算法·intellij-idea
今天吃饺子1 小时前
2024年SCI一区最新改进优化算法——四参数自适应生长优化器,MATLAB代码免费获取...
开发语言·算法·matlab
努力进修1 小时前
“探索Java List的无限可能:从基础到高级应用“
java·开发语言·list